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_)
相关推荐
Petrichor_H_13 分钟前
DAY 31 文件的规范拆分和写法
python
咚咚王者1 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
深蓝海拓2 小时前
使matplot显示支持中文和负号
开发语言·python
AntBlack2 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
一眼万里*e2 小时前
搭建本地deepseek大模型
python
1***Q7842 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python
二进制的Liao2 小时前
【编程】脚本编写入门:从零到一的自动化之旅
数据库·python·算法·自动化·bash
Dxy12393102163 小时前
Python为什么要使用可迭代对象
开发语言·python
Keep_Trying_Go3 小时前
论文STEERER人群计数,车辆计数以及农作物计数算法详解(pytorch)
人工智能·pytorch·python
gzu_013 小时前
基于昇腾 配置pytorch环境
人工智能·pytorch·python