from PIL import Image
import imagehash
def get_phash(file_path):
img = Image.open(file_path)
phash = imagehash.phash(img).hash.flatten()
phash_list = list(map(bool, phash))
if phash_list[0] is True:
# 取反
for k, v in enumerate(phash_list):
if k == 0:
continue
phash_list[k] = not v
ret = []
add = 1
for i in phash_list[1:][::-1]:
if i + add > 1:
ret.append(0)
add = 1
else:
ret.append(i + add)
add = 0
return sum((2 ** k) * v for k, v in enumerate(ret)) * -1
return sum((2 ** k) * v for k, v in enumerate(phash_list[::-1]))