首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试对ImageHash库使用差异散列时出现Numpy错误

尝试对ImageHash库使用差异散列时出现Numpy错误
EN

Stack Overflow用户
提问于 2021-04-27 09:30:36
回答 1查看 116关注 0票数 0

我尝试使用python库执行差分哈希运算,但一直收到ImageHash错误。

错误:

文件图像第252行,在dhash "/Users/testuser/Desktop/test_folder/test_env/lib/python3.8/site-packages/imagehash.py",= image.convert("L").resize((hash_size + 1,hash_size),Image.ANTIALIAS) AttributeError:'numpy.ndarray‘对象没有'convert’属性。

代码:

代码语言:javascript
复制
from PIL import Image
from cv2 import cv2
import imagehash
import numpy as np

def hash_and_compare(image1, image2):
    image1 = image1
    image2 = image2

    # read images
    image1 = cv2.imread(image1)
    image2 = cv2.imread(image2)

    # convert to grayscale
    image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

    # resize images
    image1 = cv2.resize(image1, (9, 8))
    image2 = cv2.resize(image2, (9, 8))

    # hash images
    image1_hash = imagehash.dhash(image1)
    image2_hash = imagehash.dhash(image2)

    # compute hamming distance
    distance = image1_hash - image2_hash

    if image1_hash <= 10:
        print(distance)
        print('match')
    else:
        print(distance)
        print('no match')

hash_and_compare('/Users/testuser/Desktop/test_folder/game_name056496.png', '/Users/testuser/Desktop/test_folder/game_name499761.png')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-27 12:42:08

正如imagehash库的文档@image must be a PIL instance.中所提到的。所以你不能设置numpy数组作为dshash function.if的输入,你想用opencv做一些预处理,你应该在设置成dhash之前把它转换成PIL数组,如下所示:

代码语言:javascript
复制
import numpy as np
from PIL import Image
...
some preprocess
...
# You may need to convert the color.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
image1_hash = imagehash.dhash(im_pil)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67275803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档