Python-图片去重

直接上代码

python 复制代码
# 修改一下第34行文件夹路径以及13行图片后缀名即可使用
import os
from hashlib import md5


def remove_duplicate_images(folder_path):
    image_files = []
    duplicate_images = set()

    # 遍历文件夹,找到所有 JPG 图片文件
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.lower().endswith('.jpg'):
                image_files.append(os.path.join(root, file))

    # 遍历所有图片文件,并使用哈希值比较是否完全相同
    for image_file in image_files:
        with open(image_file, 'rb') as f:
            image_data = f.read()
            image_hash = md5(image_data).hexdigest()

            if image_hash in duplicate_images:
                f.close()
                # 如果哈希值存在于重复图片集合中,则删除图片文件
                print(os.path.basename(image_file))
                os.remove(image_file)
            else:
                # 否则将哈希值添加到重复图片集合中
                duplicate_images.add(image_hash)
                f.close()


# 文件夹路径
folder_path_ = 'E:/古风/'

# 调用函数删除重复图片
remove_duplicate_images(folder_path_)
相关推荐
@sinner16 分钟前
《扫雷:病毒蔓延》- 颠覆传统的动态扫雷游戏
python·游戏·pygame
愈努力俞幸运16 分钟前
python 列表浅拷贝 深拷贝
python
测试老哥28 分钟前
测试用例之正交试验法、功能图法
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
岁岁岁平安1 小时前
python基本数据类型、字典、 集合、条件与循环控制、函数(3)
python·学习·集合·函数·字典·python3
wan了个蛋1 小时前
使用python脚本大批量自动化处理图片上的ai水印
python
好家伙VCC2 小时前
**TensorFlow:发散创新的深度学习框架探索**随着人工智
java·人工智能·python·深度学习·tensorflow
YFLICKERH2 小时前
【多进线程】python多进线程与通信
python
Metaphor6922 小时前
Java 获取 Excel 中工作表的名称
经验分享
程序员爱钓鱼2 小时前
Python编程实战 · 基础入门篇 | 第一个Python程序:Hello World
后端·python·编程语言
川石课堂软件测试3 小时前
CSS中常用的几种定位。
开发语言·css·python·网络协议·http·html·pytest