Python 图像哈希

2024-01-01

我目前正在尝试从 python 中的图像获取哈希值,我已经成功完成了此操作并且它有效somewhat.

但是,我有这个问题: Image1 和 image2 最终具有相同的哈希值,尽管它们不同。我需要一种更准确和精确的散列形式。

图片1 =Image1 https://i.stack.imgur.com/e3FsG.png

图片2=Image2 https://i.stack.imgur.com/MirOx.png

图像的哈希值是:faf0761493939381

我目前正在使用from PIL import Image import imagehash

And imagehash.average_hash

代码在这里

import os
from PIL import Image
import imagehash


def checkImage():
    for filename in os.listdir('images//'):
        hashedImage = imagehash.average_hash(Image.open('images//' + filename))
    print(filename, hashedImage)

    for filename in os.listdir('checkimage//'):
        check_image = imagehash.average_hash(Image.open('checkimage//' + filename))
    print(filename, check_image)

    if check_image == hashedImage:
        print("Same image")
    else:
        print("Not the same image")

    print(hashedImage, check_image)


checkImage()

尝试使用hashlib https://docs.python.org/3/library/hashlib.html。只需打开文件并执行哈希即可。

import hashlib
# Simple solution
with open("image.extension", "rb") as f:
    hash = hashlib.sha256(f.read()).hexdigest()
# General-purpose solution that can process large files
def file_hash(file_path):
    # https://stackoverflow.com/questions/22058048/hashing-a-file-in-python

    sha256 = hashlib.sha256()

    with open(file_path, "rb") as f:
        while True:
            data = f.read(65536) # arbitrary number to reduce RAM usage
            if not data:
                break
            sha256.update(data)

    return sha256.hexdigest()

感谢 Antonín Hoskovec 指出它应该被读取为二进制(rb),不是简单的读(r)!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 图像哈希 的相关文章

随机推荐