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_)
相关推荐
Wpa.wk12 分钟前
接口测试-Postman接口测试小练习-初级
经验分享·测试工具·接口测试·postman
历程里程碑14 分钟前
破解三数之和:双指针高效解法
c语言·数据结构·c++·经验分享·算法·leetcode·排序算法
小智RE0-走在路上17 分钟前
Python学习笔记(13) --Mysql,Python关联数据库
数据库·python·学习
YJlio19 分钟前
杨利杰YJlio|博客导航目录(专栏总览 + 推荐阅读路线)
开发语言·python·pdf
Swizard19 分钟前
数据不够代码凑?用 Albumentations 让你的 AI 模型“看”得更广,训练快 10 倍!
python·算法·ai·训练
智算菩萨23 分钟前
【Python机器学习】决策树与随机森林:解释性与鲁棒性的平衡
python·决策树·机器学习
宁大小白23 分钟前
pythonstudy Day44
python·机器学习
羸弱的穷酸书生38 分钟前
今天跟 AI学了一手
python
不要em0啦39 分钟前
从0开始学python:声明
python
ha_lydms41 分钟前
4、Spark 函数_m/n/o/p/q/r
大数据·数据库·python·sql·spark·数据处理·dataworks