2015-05-16 60 views
2

我正在使用ImageHash模塊獲取圖像的哈希值。我有這樣的代碼:Python中zfill方法的說明

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是該模塊的基本用法。

現在我不明白的是hashSizeimagehash2中原始字符串所做的轉換以及第3個函數如何將imagehash2轉換爲64位字符串。

+0

*「這一行」 *是什麼線?請不要寫這樣一個毫無意義的標題,然後通過發佈多行來使其錯誤。 –

+0

我很抱歉,但我真的不知道如何描述它。你能給我一個更明確的標題嗎? – Hyperion

回答

1

phash期間計算原始圖像的大小。 hashSize參數基本上控制調整大小的圖像的高度和寬度。

算法可以找到here。第一步的執行情況(減少大小):

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' 

降價使用list slice前兩個字符

In [19]: bin(int(imghash2, 16))[2:] 
Out[19]: '10001101110010111110001111110101100010101100010101100' 
左側

Adds 0做出的64個字符的字符串總

In [20]: bin(int(imghash2, 16))[2:].zfill(64) 
Out[20]: '0000000000010001101110010111110001111110101100010101100010101100'