zoukankan      html  css  js  c++  java
  • python 判断 图片是否相同

    import os
        from PIL import Image
        from PIL import ImageFile
        import imagehash
          
        #仅适用于图片内容完全相同,而尺寸不同的比较
        def compare_image(img_file1, img_file2):
            if img_file1 == img_file2:
                return True
            fp1 = open(img_file1, 'rb')
            fp2 = open(img_file2, 'rb')
        
            img1 = Image.open(fp1)
            img2 = Image.open(fp2)
        
            ImageFile.LOAD_TRUNCATED_IMAGES = True
            b = img1 == img2
        
            fp1.close()
            fp2.close()
        
            return b
        
    
    
    
    
        #通过图片hash值进行比较
        def get_hash_dict(dir):
            hash_dict = {}
            image_quantity = 0
            for _, _, files in os.walk(dir):
                for i, fileName in enumerate(files):
                    with open(dir + fileName, 'rb') as fp:
                        hash_dict[dir + fileName] = imagehash.average_hash(Image.open(fp))
                        image_quantity += 1
        
            return hash_dict, image_quantity
        
        def compare_image_with_hash(image_file_name_1, image_file_name_2, max_dif=0):
            """
            max_dif: 允许最大hash差值, 越小越精确,最小为0
            推荐使用
            """
            ImageFile.LOAD_TRUNCATED_IMAGES = True
            hash_1 = None
            hash_2 = None
            with open(image_file_name_1, 'rb') as fp:
                hash_1 = imagehash.average_hash(Image.open(fp))
            with open(image_file_name_2, 'rb') as fp:
                hash_2 = imagehash.average_hash(Image.open(fp))
            dif = hash_1 - hash_2
            if dif < 0:
                dif = -dif
            if dif <= max_dif:
                return True
            else:
                return False
        
        
        def compare_image_dir_with_hash(dir_1, dir_2, max_dif=0):
            """
            max_dif: 允许最大hash差值, 越小越精确,最小为0
            用于比较两个文件夹内的图片
            """
            ImageFile.LOAD_TRUNCATED_IMAGES = True
            hash_dict_1, image_quantity_1 = get_hash_dict(dir_1)
            hash_dict_2, image_quantity_2 = get_hash_dict(dir_2)
        
            if image_quantity_1 > image_quantity_2:
                tmp = image_quantity_1
                image_quantity_1 = image_quantity_2
                image_quantity_2 = tmp
        
                tmp = hash_dict_1
                hash_dict_1 = hash_dict_2
                hash_dict_2 = tmp
        
            result_dict = {}
        
            for k in hash_dict_1.keys():
                result_dict[k] = None
        
            for dif_i in range(0, max_dif + 1):
                have_none = False
        
                for k_1 in result_dict.keys():
                    if result_dict.get(k_1) is None:
                        have_none = True
        
                if not have_none:
                    return result_dict
        
                for k_1, v_1 in hash_dict_1.items():
                    for k_2, v_2 in hash_dict_2.items():
                        sub = (v_1 - v_2)
                        if sub < 0:
                            sub = -sub
                        if sub == dif_i and result_dict.get(k_1) is None:
                            result_dict[k_1] = k_2
                            break
            return result_dict
        
        
        def main():
            print(compare_image('image1\815.jpg', 'image2\5.jpg'))
            print(compare_image_with_hash('image1\815.jpg', 'image2\5.jpg', 6))
            r = compare_image_dir_with_hash('image1\', image2\', 10)
            for k in r.keys():
                print(k, r.get(k))
        
        
        if __name__ == '__main__':
            main()
    

    运行结果:

        False
        True
        image25.jpg image1815.jpg
        image26.jpg image1819.jpg
        image27.jpg image1900.jpg
        image28.jpg image1998.jpg
        image29.jpg image11012.jpg
    

    图片例子

    • 815.jpg

    • 5.jpg

  • 相关阅读:
    INewPolygonFeedback接口(实时显示所画多边形的面积)
    RichTextBox追加文本信息,并根据信息的多少自动下移
    Google tile和TMS的索引算法
    出差咸阳
    失败的 炸羊排
    年中总结
    Oracle 两个表之间的数据更新
    ArcEngine下,实现图形的擦除操作(Erase操作)
    C#中使用ListView动态添加数据不闪烁(转)
    出差略阳
  • 原文地址:https://www.cnblogs.com/twfb/p/6889511.html
Copyright © 2011-2022 走看看