使用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 随机生成中文验证码的实例代码
Mar 20 Python
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
Apr 01 Python
Python日期时间对象转换为字符串的实例
Jun 22 Python
详解Python中pandas的安装操作说明(傻瓜版)
Apr 08 Python
解决Python内层for循环如何break出外层的循环的问题
Jun 24 Python
Python 多个图同时在不同窗口显示的实现方法
Jul 07 Python
python如何实现异步调用函数执行
Jul 08 Python
Python图像处理PIL各模块详细介绍(推荐)
Jul 17 Python
Python人工智能之路 之PyAudio 实现录音 自动化交互实现问答
Aug 13 Python
学习python需要有编程基础吗
Jun 02 Python
pandas.DataFrame.drop_duplicates 用法介绍
Jul 06 Python
如何利用python进行时间序列分析
Aug 04 Python
python学习之panda数据分析核心支持库
Python基于Tkinter开发一个爬取B站直播弹幕的工具
May 06 #Python
Python爬虫之爬取最新更新的小说网站
May 06 #Python
Python基础之操作MySQL数据库
Python 如何安装Selenium
Django实现在线无水印抖音视频下载(附源码及地址)
Django给表单添加honeypot验证增加安全性
You might like
一个阿拉伯数字转中文数字的函数
2006/10/09 PHP
模仿OSO的论坛(三)
2006/10/09 PHP
php 中文字符入库或显示乱码问题的解决方法
2010/04/12 PHP
Windows 下的 PHP-PEAR 安装方法
2010/11/20 PHP
php实现姓名根据首字母排序的类与方法(实例代码)
2018/05/16 PHP
Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法
2018/08/16 PHP
javascript 节点遍历函数
2010/03/28 Javascript
2014 年最热门的21款JavaScript框架推荐
2014/12/25 Javascript
了解Javascript的模块化开发
2015/03/02 Javascript
JavaScript转换与解析JSON方法实例详解
2015/11/24 Javascript
全面解析JavaScript的Backbone.js框架中的Router路由
2016/05/05 Javascript
JavaScript 中对象的深拷贝
2016/12/04 Javascript
浅谈react.js中实现tab吸顶效果的问题
2017/09/06 Javascript
Angularjs使用过滤器完成排序功能
2017/09/20 Javascript
JS去掉字符串中所有的逗号
2017/10/18 Javascript
javascript trie前缀树的示例
2018/01/29 Javascript
vue组件中使用props传递数据的实例详解
2018/04/08 Javascript
Vue 组件修改根实例的数据的方法
2019/04/02 Javascript
Vue 实现把表单form数据 转化成json格式的数据
2019/10/29 Javascript
Vuex模块化应用实践示例
2020/02/03 Javascript
[02:06]DOTA2肉山黑名单魔法终结者 敌法师中文配音鉴赏
2013/06/17 DOTA
python获取标准北京时间的方法
2015/03/24 Python
python实现搜索指定目录下文件及文件内搜索指定关键词的方法
2015/06/28 Python
详解Python 定时框架 Apscheduler原理及安装过程
2019/06/14 Python
Python对接六大主流数据库(只需三步)
2019/07/31 Python
pygame实现俄罗斯方块游戏(基础篇3)
2019/10/29 Python
详解从Django Allauth中进行登录改造小结
2019/12/18 Python
Python对象的属性访问过程详解
2020/03/05 Python
详解html2canvas截图不能截取圆角图片的解决方案
2018/01/30 HTML / CSS
西班牙家用电器和电子产品购物网站:Mi Electro
2019/02/25 全球购物
西班牙购买隐形眼镜、眼镜和太阳镜网站:Lentiamo.es
2020/06/11 全球购物
长青弘远的面试题
2012/06/09 面试题
幼儿园新学期寄语
2014/01/18 职场文书
机关班子查摆问题及整改措施
2014/10/28 职场文书
计生个人工作总结
2015/02/28 职场文书
Go缓冲channel和非缓冲channel的区别说明
2021/04/25 Golang