Python中zfill方法的说明
我正在使用模块获取图像的哈希值。我有以下代码:Python中zfill方法的说明,python,hash,Python,Hash,我正在使用模块获取图像的哈希值。我有以下代码: 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) >>>
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位字符串。在phash
计算期间,原始图像的大小被调整hashSize
参数主要控制调整大小图像的高度和宽度
算法可以找到。第一步的实施(减小尺寸):
见资料来源
让我们看看行
imghash3.append(bin(int(imghash2,16))[2:].zfill(64))
的作用
In [16]: imghash2 = '11b97c7eb158ac'
首先,它将十六进制字符串转换为整数
In [17]: int(imghash2, 16)
Out[17]: 4989018956716204
内置函数用于将整数转换为二进制字符串
In [18]: bin( int(imghash2, 16))
Out[18]: '0b10001101110010111110001111110101100010101100010101100'
使用删除前两个字符
在左侧创建一个总共64个字符的字符串
In [20]: bin( int(imghash2, 16))[2:].zfill(64)
Out[20]: '0000000000010001101110010111110001111110101100010101100010101100'
“这条线”是什么线?请不要写这样一个毫无意义的标题,甚至连一行字都写错。很抱歉,我真的不知道如何描述它。你能给我一个更清楚的标题吗?
In [19]: bin( int(imghash2, 16))[2:]
Out[19]: '10001101110010111110001111110101100010101100010101100'
In [20]: bin( int(imghash2, 16))[2:].zfill(64)
Out[20]: '0000000000010001101110010111110001111110101100010101100010101100'