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_)
相关推荐
Mr.D学长6 分钟前
毕业设计 深度学习社交距离检测系统(源码+论文)
python·毕业设计·毕设
wdxylb10 分钟前
解决Python使用Selenium 时遇到网页 <body> 划不动的问题
python
代码骑士18 分钟前
【一起学NLP】Chapter3-使用神经网络解决问题
python·神经网络·自然语言处理
wxin_VXbishe1 小时前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
ITenderL1 小时前
Python学习笔记-函数
python·学习笔记
zmjia1111 小时前
全流程Python编程、机器学习与深度学习实践技术应用
python·深度学习·机器学习
_.Switch2 小时前
Python机器学习:自然语言处理、计算机视觉与强化学习
python·机器学习·计算机视觉·自然语言处理·架构·tensorflow·scikit-learn
JUNAI_Strive_ving2 小时前
番茄小说逆向爬取
javascript·python
彤银浦2 小时前
python学习记录7
python·学习
简单.is.good3 小时前
【测试】接口测试与接口自动化
开发语言·python