使用python如何删除同一文件夹下相似的图片
作者:MHyourh 发布时间:2021-10-19 02:52:23
标签:python,删除,图片
前言
最近整理图片发现,好多图片都非常相似,于是写如下代码去删除,有两种方法:
注:第一种方法只对于连续图片(例一个视频里截下的图片)准确率也较高,其效率高;第二种方法准确率高,但效率低
方法一:相邻两个文件比较相似度,相似就把第二个加到新列表里,然后进行新列表去重,统一删除。
例如:有文件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)
来源:https://blog.csdn.net/sinat_38682860/article/details/103498657
0
投稿
猜你喜欢
- 本文介绍在Anaconda环境中,安装Python语言pydot与graphviz两个模块的方法。最近进行随机森林(RF)的树的可视化操作,
- 1、存储过程基本语法: create procedure sp_name() begin ...... end; 2、如何调用: call
- 方法一:(by yangedie )这几天刚刚做了这个东西,有网友问到,所以分享一下。ie6、firefox2 通过,麻烦有ie7的网友测试
- 一、什么是函数装饰器1.函数装饰器是Python提供的一种增强函数功能的标记函数;2.装饰器是可调用的函数对象,其参数是另一个函数(被装饰的
- 前言上篇说到命令行执行测试用例的部分参数如何使用?今天将继续更新其他一些命令选项的使用,和pytest收集测试用例的规则!pytest执行用
- 问题详情:使用pip install pyecharts 安装的是最新版,本人默认回车后安装1.1.0版本,出现如图问题:解决方法:(推荐第
- 一组有序项目的集合可变的数据类型【可进行增删改查】列表中可以包含任何数据类型,也可包含另一个列表【可任意组合嵌套】列表是以方括号“[]”包围
- 平时制作页面中可对属性list-style在list-item对象中常用,但用的都不深。一般都设为none重置整个页面就差不多OK,可能很多
- sort()方法排序列表中的对象,比较使用func(如果给定)。语法以下是sort()方法的语法:list.sort([func
- 1.数组上的迭代NumPy 包含一个迭代器对象numpy.nditer。它是一个有效的多维迭代器对象,可以用于在数组上进行迭代。数组的每个元
- 代码如下:---ntext数据类型字符替换 create table tt ( sid INT IDENTITY(1,1), c
- 有些页面并不能直接用requests获取到内容,会动态执行一些js代码生成内容。这个文章主要是对付那些特殊页面的,比如必须要进行js调用才能
- 使用threading写的一个定时器任务demo:import timeimport sysimport signalimport date
- 一、前言 说实话,刚测试ES的时候,我的内心是崩溃的,好多单词都不知道
- 问题你想使用一个简单的REST接口通过网络远程控制或访问你的应用程序,但是你又不想自己去安装一个完整的web框架。解决方案构建一个REST风
- 前言Go大概2009年面世以来,已经8年了,也算是8年抗战。在这8年中,已经有很多公司开始使用Go语言开发自己的服务,甚至完全转向Go开发,
- php遍历一个文件夹内的所有文件和文件夹,并删除所有文件夹和子文件夹下的所有文件的代码,通过递归方式实现达到清空一个目录的效果,代码简单实用
- 在 python 开发中我们最常用的IDE就是PyCharm,有关PyCharm的优点这里就不在赘述。在项目开发中我们经常用到许多第三方库,
- 本篇阅读的代码片段来自于30-seconds-of-python。1. count_bydef count_by(arr, fn=lambd
- matplotlib中的字体文件被封装在font_manager这个子模块中,fontManager.ttflist这个列表涵盖了所有Mat