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()

总结

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

相关推荐
张人玉2 小时前
茶树 NeRF(Neural Radiance Fields,神经辐射场)三维重建与数据分析可视化系统——基于神经辐射场的茶树三维重建方法实现可视化大屏
python·数据挖掘·数据分析·echarts·three.js
脉动数据行情12 小时前
Python WebSocket 国内商品期货|螺纹钢 & 铁矿石实时行情监听
开发语言·python·websocket
circuitsosk2 小时前
智能体任务拆解与执行:基于ReAct+Plan-and-Execute框架的行业Skill构建实录
前端·javascript·python·react.js·react·llm agent·智能体编排
麻雀飞吧2 小时前
近期量化工具怎么选,先看你卡在哪一环
人工智能·python
通信仿真爱好者2 小时前
第【109】期--基于神经网络的OFDM峰均功率比降低方法--python完整代码
python·神经网络·ofdm·限幅滤波·峰均功率比·papr降低
java1234_小锋3 小时前
YOLO26 计算机视觉 - YOLO26 简介 & Hello World项目搭建
人工智能·yolo·计算机视觉·机器视觉·yolo26
阿图灵3 小时前
OpenCV 图像特征与匹配:SIFT 特征检测与 BFMatcher 暴力匹配
图像处理·人工智能·python·opencv·计算机视觉·sift
王志来137944730083 小时前
工控服务器机箱选型决策要素解析:匀天以“快全准省”构建价值坐标
运维·服务器·python·devops
阿图灵3 小时前
OpenCV 实用技巧三连:视频转图片、图片转视频、Pillow 与 OpenCV 互转
图像处理·python·opencv·计算机视觉·音视频·pillow
GlueNa2SiO33 小时前
05-Flask表单处理与文件上传
笔记·python·学习·flask