python:删除文件夹里的相同图片

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


需求

删除文件夹中的相同图片,保留一个即可,还希望它运行的很快,最好是多线程


直接上代码(有注释)

python 复制代码
# coding=utf-8
import os
import shutil
from loguru import logger
import threading


def match_img(img1,img2):
	#  比较图片相似度函数 值为1.0代表两图相同
    # 计算单通道的直方图的相似值  图片相似度比较
    import cv2
    def calculate(image1, image2):
        import cv2
        hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
        hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
        # 计算直方图的重合度
        degree = 0
        for i in range(len(hist1)):
            if hist1[i] != hist2[i]:
                degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
            else:
                degree = degree + 1
        degree = degree / len(hist1)
        return degree

    # 通过得到RGB每个通道的直方图来计算相似度
    def classify_hist_with_split(image1, image2, size=(256, 256)):
        # 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
        import cv2
        image1 = cv2.resize(image1, size)
        image2 = cv2.resize(image2, size)
        sub_image1 = cv2.split(image1)
        sub_image2 = cv2.split(image2)
        sub_data = 0
        for im1, im2 in zip(sub_image1, sub_image2):
            sub_data += calculate(im1, im2)
        sub_data = sub_data / 3
        return sub_data

    n = classify_hist_with_split(cv2.imread(img1), cv2.imread(img2))
    if n == 1.0:
        return n
    return n[0]

def copy_image(source_path, destination_path):
    shutil.copy(source_path, destination_path)

class MatchImg(object):

    def __init__(self):
        self.thread_lock = threading.Lock()

    def split_list(self,my_list):
        # 5000图片为一组,一组为一个线程
        chunk_size = 5000
        chunks = [my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]
        return chunks

    def spilt_img(self,img_list,n):
    	# 一个列表,桉顺序取出一个图片跟之后图片一一比较的方式 方法比较笨
        num = len(img_list)
        for i in range(num):
            logger.debug('第{}线程 第{}轮'.format(n,i))
            img = img_list[i]
            other_imgs = img_list[i + 1:]
            if other_imgs and os.path.exists('ct_imgs/' + img):
                self.for_img(img, other_imgs,n)

    def for_img(self,img,img_list,num):
        img_path = 'ct_imgs/'+img
        if os.path.exists(img_path):
            for i in img_list:
                i_img = 'ct_imgs/' + i
                if os.path.exists(i_img):
                    res = match_img(img_path,i_img)
                    if res == 1.0:
                        self.thread_lock.acquire()
                        logger.debug('第{}线程 删除图片路径 {}'.format(num,i_img))
                        os.remove(i_img)
                        self.thread_lock.release()

    def run(self):
        folder_path = 'ct_imgs' # 目标文件夹路径
        img_list = os.listdir(folder_path)
        print(len(img_list))
        pool = []
        split_list = self.split_list(img_list)
        for i in range(len(split_list)):
            t = threading.Thread(target=self.spilt_img,args=(split_list[i],i))
            t.start()
            pool.append(t)
        for j in pool:
            j.join()

if __name__ == '__main__':
    obj = MatchImg()
    obj.run()

总结

本代码对比算法比较中庸,欢迎指出更好的对比算法,该程序面对数据多的图片文件根据实际情况需要多次运行处理,因为这个是组内图片比较。

相关推荐
IVEN_15 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang16 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮16 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling16 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮20 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽20 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞2 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽2 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers