ficapy
3/4/2018 - 1:16 AM

将phash得到的64位数据转换成bigint方便存储到数据库

将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]))