Python3 比較兩個圖片是否類似或相同


  Python代碼

 #coding:utf8
import os
from PIL import Image,ImageDraw,ImageFile
import numpy
import pytesseract
import cv2
import imagehash
import collections

def compare_image_with_hash(self,image_file1="D:\work\python36_crawl\Veriycode\mode_5246_1.png",
                                image_file2="D:\work\python36_crawl\Veriycode\mode_5246_2.png", max_dif=0):
        """
        max_dif: 允許最大hash差值, 越小越精確,最小為0
        推薦使用
        """
        ImageFile.LOAD_TRUNCATED_IMAGES = True
        hash_1 = None
        hash_2 = None
        with open(image_file1, 'rb') as fp:
            hash_1 = imagehash.average_hash(Image.open(fp))
            print(hash_1)
        with open(image_file2, 'rb') as fp:
            hash_2 = imagehash.average_hash(Image.open(fp))
            print(hash_2)
        dif = hash_1 - hash_2
        print(dif)
        if dif < 0:
            dif = -dif
        if dif <= max_dif:
            return True
        else:
            return False

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM