使用python如何删除同一文件夹下相似的图片


Posted in Python onMay 07, 2021

前言

最近整理图片发现,好多图片都非常相似,于是写如下代码去删除,有两种方法:

注:第一种方法只对于连续图片(例一个视频里截下的图片)准确率也较高,其效率高;第二种方法准确率高,但效率低

方法一:相邻两个文件比较相似度,相似就把第二个加到新列表里,然后进行新列表去重,统一删除。

例如:有文件1-10,首先1和2相比较,若相似,则把2加入到新列表里,再接着2和3相比较,若不相似,则继续进行3和4比较…一直比到最后,然后删除新列表里的图片

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.measure import compare_ssim
# import shutil
# def yidong(filename1,filename2):
#     shutil.move(filename1,filename2)
def delete(filename1):
    os.remove(filename1)
if __name__ == '__main__':
    path = r'D:\camera_pic\test\rec_pic'
    # save_path_img = r'E:\0115_test\rec_pic'
    # os.makedirs(save_path_img, exist_ok=True)
    img_path = path
    imgs_n = []
    num = []
    img_files = [os.path.join(rootdir, file) for rootdir, _, files in os.walk(path) for file in files if
                 (file.endswith('.jpg'))]
    for currIndex, filename in enumerate(img_files):
        if not os.path.exists(img_files[currIndex]):
            print('not exist', img_files[currIndex])
            break
        img = cv2.imread(img_files[currIndex])
        img1 = cv2.imread(img_files[currIndex + 1])
        ssim = compare_ssim(img, img1, multichannel=True)
        if ssim > 0.9:
            imgs_n.append(img_files[currIndex + 1])
            print(img_files[currIndex], img_files[currIndex + 1], ssim)
        else:
            print('small_ssim',img_files[currIndex], img_files[currIndex + 1], ssim)
        currIndex += 1
        if currIndex >= len(img_files)-1:
            break
    for image in imgs_n:
        # yidong(image, save_path_img)
        delete(image)

方法二:逐个去比较,若相似,则从原来列表删除,添加到新列表里,若不相似,则继续

例如:有文件1-10,首先1和2相比较,若相似,则把2在原列表删除同时加入到新列表里,再接着1和3相比较,若不相似,则继续进行1和4比较…一直比,到最后一个,再继续,正常应该再从2开始比较,但2被删除了,所以从3开始,继续之前的操作,最后把新列表里的删除。

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.measure import compare_ssim
import shutil
import datetime
def yidong(filename1,filename2):
    shutil.move(filename1,filename2)
def delete(filename1):
    os.remove(filename1)
    print('real_time:',now_now-now)
if __name__ == '__main__':
    path = r'F:\temp\demo'
    # save_path_img = r'F:\temp\demo_save'
    # os.makedirs(save_path_img, exist_ok=True)
    for (root, dirs, files) in os.walk(path):
        for dirc in dirs:
            if dirc == 'rec_pic':
                pic_path = os.path.join(root, dirc)
                img_path = pic_path
                imgs_n = []
                num = []
                del_list = []
                img_files = [os.path.join(rootdir, file) for rootdir, _, files in os.walk(img_path) for file in files if
                             (file.endswith('.jpg'))]
                for currIndex, filename in enumerate(img_files):
                    if not os.path.exists(img_files[currIndex]):
                        print('not exist', img_files[currIndex])
                        break
                    new_cur = 0
                    for i in range(10000000):
                        currIndex1 =new_cur
                        if currIndex1 >= len(img_files) - currIndex - 1:
                            break
                        else:
                            size = os.path.getsize(img_files[currIndex1 + currIndex + 1])
                            if size < 512:
                                # delete(img_files[currIndex + 1])
                                del_list.append(img_files.pop(currIndex1 + currIndex + 1))
                            else:
                                img = cv2.imread(img_files[currIndex])
                                img = cv2.resize(img, (46, 46), interpolation=cv2.INTER_CUBIC)
                                img1 = cv2.imread(img_files[currIndex1 + currIndex + 1])
                                img1 = cv2.resize(img1, (46, 46), interpolation=cv2.INTER_CUBIC)
                                ssim = compare_ssim(img, img1, multichannel=True)
                                if ssim > 0.9:
                                    # imgs_n.append(img_files[currIndex + 1])
                                    print(img_files[currIndex], img_files[currIndex1 + currIndex + 1], ssim)
                                    del_list.append(img_files.pop(currIndex1 + currIndex + 1))
                                    new_cur = currIndex1
                                else:
                                    new_cur = currIndex1 + 1
                                    print('small_ssim',img_files[currIndex], img_files[currIndex1 + currIndex + 1], ssim)
                for image in del_list:
                    # yidong(image, save_path_img)
                    delete(image)
                    print('delete',image)

总结

到此这篇关于使用python如何删除同一文件夹下相似图片的文章就介绍到这了,更多相关python删除文件夹相似图片内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python使用reportlab模块生成PDF格式的文档
Mar 11 Python
对PyQt5中树结构的实现方法详解
Jun 17 Python
python编写简单端口扫描器
Sep 04 Python
django框架创建应用操作示例
Sep 26 Python
Python读取YAML文件过程详解
Dec 30 Python
python面向对象之类属性和类方法案例分析
Dec 30 Python
Python tornado上传文件的功能
Mar 26 Python
windows python3安装Jupyter Notebooks教程
Apr 13 Python
使用Keras预训练好的模型进行目标类别预测详解
Jun 27 Python
OpenCV图片漫画效果的实现示例
Aug 18 Python
详解python的super()的作用和原理
Oct 29 Python
python的html标准库
Apr 29 Python
python学习之panda数据分析核心支持库
Python基于Tkinter开发一个爬取B站直播弹幕的工具
May 06 #Python
Python爬虫之爬取最新更新的小说网站
May 06 #Python
Python基础之操作MySQL数据库
Python 如何安装Selenium
Django实现在线无水印抖音视频下载(附源码及地址)
Django给表单添加honeypot验证增加安全性
You might like
PHP注释实例技巧
2008/10/03 PHP
php常用ODBC函数集(详细)
2013/06/24 PHP
微信支付PHP SDK ―― 公众号支付代码详解
2016/09/13 PHP
js实现兼容IE6与IE7的DIV高度
2010/05/13 Javascript
解析javascript 实用函数的使用详解
2013/05/10 Javascript
JavaScript作用域与作用域链深入解析
2013/12/06 Javascript
JQ技术实现注册页面带有校验密码强度
2015/07/27 Javascript
jQuery过滤HTML标签并高亮显示关键字的方法
2015/08/07 Javascript
jQuery Easyui实现左右布局
2016/01/26 Javascript
Backbone.js框架中Model与Collection的使用实例
2016/05/07 Javascript
浅谈JavaScript变量的自动转换和语句
2016/06/12 Javascript
mac下的nodejs环境安装的步骤
2017/05/24 NodeJs
js实现文章目录索引导航(table of content)
2020/05/10 Javascript
JQuery实现折叠式菜单的详细代码
2020/06/03 jQuery
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
2020/12/25 Vue.js
Python中dictionary items()系列函数的用法实例
2014/08/21 Python
用ReactJS和Python的Flask框架编写留言板的代码示例
2015/12/19 Python
python pandas中对Series数据进行轴向连接的实例
2018/06/08 Python
在Pandas中给多层索引降级的方法
2018/11/16 Python
python如何获取当前文件夹下所有文件名详解
2019/01/25 Python
Python对象转换为json的方法步骤
2019/04/25 Python
python命令行工具Click快速掌握
2019/07/04 Python
Numpy与Pytorch 矩阵操作方式
2019/12/27 Python
python selenium 获取接口数据的实现
2020/12/07 Python
python 将html转换为pdf的几种方法
2020/12/29 Python
菲律宾酒店预订网站:Hotels.com菲律宾
2017/07/12 全球购物
英国建筑用品在线:Building Supplies Online(BSO)
2018/04/30 全球购物
Laura官网:加拿大女性的顶级时尚目的地
2019/09/20 全球购物
TecoBuy澳大利亚:在线电子和小工具商店
2020/06/25 全球购物
路德维希•贝克(LUDWIG BECK)中文官网:德国大型美妆百货
2020/09/19 全球购物
阿尔卡特(中国)的面试题目
2014/08/20 面试题
北京泡泡网网络有限公司.net面试题
2012/07/17 面试题
小学后勤管理制度
2014/01/14 职场文书
教师四风问题对照检查材料
2014/09/26 职场文书
唐山大地震观后感
2015/06/05 职场文书
MySQL数据迁移相关总结
2021/04/29 MySQL