我正在使用ImageHash模块获取图像的散列。我有这样的代码:
hashSize = 8
imghash3 = []
image = "pic1.jpg"
imghash1 = imagehash.phash(Image.open(image))
print(imghash1)
>>>d1d1f1f3f3737373
imghash2 = str(imagehash.phash(Image.open(image), hashSize))
print(imghash2)
>>>11b97c7eb158ac
imghash3.append(bin( int(imghash2, 16))[2:].zfill(64))
print(imghash3)
>>>['0000000000010001101110010111110001111110101100010101100010101100']因此,imagehash1是该模块的基本用法。
现在我不明白的是,hashSize对imagehash2中的原始字符串进行了什么样的转换,以及第三个函数如何将imagehash2转换为64位字符串。
发布于 2015-05-16 13:31:00
在phash计算过程中,调整原始图像的大小。hashSize参数基本上控制调整大小的图像的高度和宽度。
算法可以找到这里。实施第一步(缩小规模):
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)见imagehash.phash的来源
让我们看看行imghash3.append(bin( int(imghash2, 16))[2:].zfill(64))是干什么的。
In [16]: imghash2 = '11b97c7eb158ac'首先,它将十六进制字符串转换为整数。
In [17]: int(imghash2, 16)
Out[17]: 4989018956716204内置的bin函数用于将整数转换为二进制字符串。
In [18]: bin( int(imghash2, 16))
Out[18]: '0b10001101110010111110001111110101100010101100010101100'使用列表切片删除前两个字符
In [19]: bin( int(imghash2, 16))[2:]
Out[19]: '10001101110010111110001111110101100010101100010101100'0位于左侧,以生成总计64个字符的字符串。
In [20]: bin( int(imghash2, 16))[2:].zfill(64)
Out[20]: '0000000000010001101110010111110001111110101100010101100010101100'https://stackoverflow.com/questions/30275613
复制相似问题