首页 > 解决方案 > python:在线操作图像而不是下载它们

问题描述

我正在运行此代码来比较属于两个不同社交网络的两个个人资料图像的相似性,我首先从两个网站下载图像,然后对于每个用户,我将本地图像路径提供给两个变量,我想知道是否有任何替代方法可以让我在线操作这些图像而不是下载它们,即:提供代码图像的 URL 而不是本地路径(我不想在本地机器上下载图像,因为它会占用太多空间在处理数以百万计的人时)

from PIL import Image
import imagehash
hash0 = imagehash.average_hash(Image.open('quora_photo.jpg')) 
hash1 = imagehash.average_hash(Image.open('twitter_photo.jpeg')) 
cutoff = 5

if hash0 - hash1 < cutoff:
    print('images are similar')
else:
    print('images are not similar')

标签: pythonwebimage-processingweb-crawlerimage-manipulation

解决方案


Every time you see an image on your browser your machine has downloaded it before. It is not possible to manipulate an image without downloading it.

Take a look at tempfile module, you may create temporary files to be sure that they will be deleted in the future. Or delete the files after you manipulate them as @ArnavBorborah said

EDIT :

Take a look at this method urllib.request.urlretrieve

You can adapt their example :

import urllib.request
local_filename, headers = urllib.request.urlretrieve(<image_url>)
with open(local_filename) as image:
    #do stuff

The second argument, if present, specifies the file location to copy to (if absent, the location will be a tempfile with a generated name).

If you do not specify the filename argument, urllib will create a temporary file


推荐阅读