python - 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')
解决方案
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
推荐阅读
- symfony4 - Symfony 4 Form Builder EntityType Field "not a valid class" error
- rust - How to enumerate all integers of a given type in Rust?
- elasticsearch - 在弹性搜索中创建索引时出错
- java - How to make a picture not blurry in Java
- c# - Convert long date/time zone string to datetime
- android - Android how to detect device rotation for portrait configured activity?
- linux - Linux SIGSTOP causes waitpid to continue
- flutter - Retrive data according to App Language on Flutter
- mongodb - is there a way to check if a given field exists in pyspark and mongodb?
- c - Using strtok and trying to store their values into an array