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_)
相关推荐
winfredzhang18 小时前
用 MediaPipe 手势数字识别一键打开下载夹里的图片(Python + OpenCV 实战)
人工智能·python·opencv·google·mediapipe
小白舒_SC1 天前
多个VS版本的Qt VS Tools的QtMsBuild不兼容问题
经验分享·qt
某人辛木1 天前
Web自动化测试
前端·python·pycharm·pytest
C+++Python1 天前
详细介绍一下Java泛型的通配符
java·windows·python
小帅热爱难回头1 天前
编写Skill生成AI落地项目系统架构
python
diving deep1 天前
脚本速览-python
开发语言·python
2601_951643771 天前
Python第一,Java跌出前三,C语言杀回来了
java·c语言·python·编程语言排行·技术趋势
AC赳赳老秦1 天前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
BomanGe91 天前
NSK紧凑型精密滚珠丝杠技术手册
经验分享·规格说明书
茉莉玫瑰花茶1 天前
综合案例 - AI 智能租房助手 [ 5 ]
服务器·数据库·人工智能·python·ai