python 使用 imagehash 和 distance 相似图片识别

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41041321/article/details/78943051
#--coding:utf-8--
from PIL import Image
import imagehash
import distance

funct_hash=imagehash.phash

pil_im=Image.open(r"timg.jpg")

stepx=64
stepy=64

print pil_im.size

oneitem=[]
res_box=[]

'''
def Hamming_distance(str1,str2):
    diffs = 0
    for ch1,ch2 in zip(str1,str2):
       if ch1 != ch2:
           diffs+=1
    return diffs
'''

def split_image():
  for x in range(0,pil_im.size[0]-1,stepx):
    for y in range(0,pil_im.size[1]-1,stepy):
        box=(x,y,x+stepx,y+stepy)
        region=pil_im.crop(box)
        region.save(str(x)+str(y)+".jpg","jpeg")
        oneitem=[]
        oneitem.append(str(x)+str(y)+".jpg")
        oneitem.append(x)
        oneitem.append(y)
        oneitem.append(funct_hash(region))
        res_box.append(oneitem)

def findres(filename):
 hash2 = funct_hash(Image.open(filename))
 for item in res_box:
     hash1=item[3]
     res=distance.hamming(str(hash1),str(hash2))
     if res<3:
           print item[1],
           print item[2]


split_image();
for item in res_box:
     print item[0]
     findres(item[0])













展开阅读全文

没有更多推荐了,返回首页