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_)
相关推荐
码界奇点21 分钟前
Python内置函数全解析:30个核心函数语法、案例与最佳实践指南
linux·服务器·python
dreams_dream38 分钟前
django错误记录
后端·python·django
MC皮蛋侠客1 小时前
使用Python实现DLT645-2007智能电表协议
python·网络协议·tcp/ip·能源
中等生1 小时前
Python 的循环引入问题
python
站大爷IP2 小时前
Python字符串全解析:从基础操作到高级技巧
python
中等生2 小时前
FastAPI vs Flask 性能对比:异步的真正优势在哪里?
python
DFT计算杂谈2 小时前
EPWpy教程:一个脚本完成能带、声子、电声耦合、弛豫时间计算
python
hui函数3 小时前
Flask蓝图:模块化开发的利器
后端·python·flask
跟橙姐学代码3 小时前
Python 装饰器超详细讲解:从“看不懂”到“会使用”,一篇吃透
前端·python·ipython
站大爷IP3 小时前
Rust爬虫实战:用reqwest+select打造高效网页抓取工具
python