在图像上执行dhash时获取不同的哈希长度

2024-05-16 00:54:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试计算图像的哈希值。 我在相同尺寸的图像上使用dhash,但得到的哈希长度不同(即使使用填充)

为什么呢

def dhash(image, hash_size=16):
   image = image.convert("L").resize((hash_size + 1, hash_size),
                                  Image.ANTIALIAS)
   pixels = np.asarray(image)
   diff = pixels[:, 1:] > pixels[:, :-1]
   bit_string = ''.join(str(b) for b in 1 * diff.flatten())
   return int(bit_string, 2)
image1.size
(1280, 800)
image2.size
(1280, 800)
hash1 = dhash(image1)
hash2 = dhash(image2)
bin(hash1 ).zfill(64)
'0b1001101000000000100110100000000000010000000000000010101100000000001011111000000011011011100000001001111110000000100101111000000111011011100000011001101110000000100110111000000010011011100000001101100110000000100110111100100010011011010'
bin(hash2 ).zfill(64)
'0b101101000000000010110100000000001011011100000000101101000111000110110100101100001011011001110001101101000111000110110100101100011011011111001000001101001011010110110100101101001011011101001000101101000111000010110100000000001011010000000000101101000000000'

Tags: 图像imagesizestringbinbitdiffhash