Python代码
1#coding:utf8 2import os 3from PIL import Image,ImageDraw,ImageFile 4import numpy 5import pytesseract 6import cv2 7import imagehash 8import collections 9 10def compare_image_with_hash(self,image_file1="D:\work\python36_crawl\Veriycode\mode_5246_1.png", 11 image_file2="D:\work\python36_crawl\Veriycode\mode_5246_2.png", max_dif=0): 12 """ 13 max_dif: 允许最大hash差值, 越小越精确,最小为0 14 推荐使用 15 """ 16 ImageFile.LOAD_TRUNCATED_IMAGES = True 17 hash_1 = None 18 hash_2 = None 19 with open(image_file1, 'rb') as fp: 20 hash_1 = imagehash.average_hash(Image.open(fp)) 21 print(hash_1) 22 with open(image_file2, 'rb') as fp: 23 hash_2 = imagehash.average_hash(Image.open(fp)) 24 print(hash_2) 25 dif = hash_1 - hash_2 26 print(dif) 27 if dif < 0: 28 dif = -dif 29 if dif <= max_dif: 30 return True 31 else: 32 return False