我想从大约 10.000.000 张图片中计算出 phash,从中我只有它们所在的 url。
我知道如何下载图片,然后计算 phash,但我总是必须先保护图片。
是否可以在不保存的情况下下载图片并计算 phash,或者甚至可以根本不下载图片而仅使用 url 计算 phash?
这是我下载前十张图片并计算 phash 的代码:
folder, pic_savefolder = 'data', 'data/pictures'
file = 'external-asset-url-clean.csv'
path = os.path.join(folder,file)
df = pd.read_csv(path, header=None, names=["URL"])
counter = 0
hashes = set()
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
for image_url in df['URL']:
filename = image_url.split('/')[-1]
try:
r = requests.get(image_url, allow_redirects=False,verify=False, headers=headers)
pathlong = os.path.join(pic_savefolder, filename)
with open(pathlong,"wb") as f:
f.write(r.content)
hash = imagehash.phash(Image.open(pathlong))
hashes.add((hash))
counter += 1
if counter > 10:
break
except Exception as e:
print(e)
print("\n")