Skip to content

Instantly share code, notes, and snippets.

@ficapy
Last active May 12, 2019 13:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ficapy/e7012d193c5ea85110a404c006ed7273 to your computer and use it in GitHub Desktop.
Save ficapy/e7012d193c5ea85110a404c006ed7273 to your computer and use it in GitHub Desktop.
将phash得到的64位数据转换成bigint方便存储到数据库
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]))
@ZZZWY
Copy link

ZZZWY commented Apr 11, 2019

你好, 看到了你的bk树索引汉明距离的文章, 有些问题想问下, 方便给下联系方式么

@ficapy
Copy link
Author

ficapy commented May 12, 2019

你好, 看到了你的bk树索引汉明距离的文章, 有些问题想问下, 方便给下联系方式么

有什么问题可以直接说, 主要就是安装一个PG索引扩展插件就可以了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment