Python -找到两个文件夹中内容相同的图

可以用哈希函数计算图像内容的哈希值: 

from PIL import Image
import os
import hashlib

def calculate_hash(image_path):
    # 使用 MD5 哈希函数计算图像内容的哈希值
    with open(image_path, "rb") as f:
        image_hash = hashlib.md5(f.read()).hexdigest()
    return image_hash

def find_same_content_images(folder1, folder2):
    images1 = [os.path.join(folder1, img) for img in os.listdir(folder1) if img.endswith(('.png', '.jpg', '.jpeg'))]
    images2 = [os.path.join(folder2, img) for img in os.listdir(folder2) if img.endswith(('.png', '.jpg', '.jpeg'))]
    
    hash_dict = {}

    for img_path in images1 + images2:
        img_hash = calculate_hash(img_path)

        if img_hash in hash_dict:
            hash_dict[img_hash].append(img_path)
        else:
            hash_dict[img_hash] = [img_path]

    # 提取内容相同的图像
    same_content_images = [img_paths for img_paths in hash_dict.values() if len(img_paths) > 1]

    return same_content_images

if __name__ == "__main__":

    folder1_path='C:/Users/Administrator/Desktop/mengniu_image_all_1/'
    folder2_path='C:/Users/Administrator/Desktop/mengniu/'

    same_content_images = find_same_content_images(folder1_path, folder2_path)

    if same_content_images:
         print("内容相同的图像:", same_content_images)
    else:
         print("两个文件夹中的图像内容都是唯一的。")
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值