python-----删除同一文件夹下相似的图片
最近整理图片发现,好多图片都非常相似,于是写如下代码去删除,有两种方法:
注:第一种方法只对于连续图片(例一个视频里截下的图片)准确率也较高,其效率高;第二种方法准确率高,但效率低
方法一:相邻两个文件比较相似度,相似就把第二个加到新列表里,然后进行新列表去重,统一删除。
例如:有文件1-10,首先1和2相比较,若相似,则把2加入到新列表里,再接着2和3相比较,若不相似,则继续进行3和4比较...一直比到最后,然后删除新列表里的图片
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/15 9:19
# @Author : xiaodai
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 -*-
# @Time : 2019/1/16 12:03
# @Author : xiaodai
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读取不同文件夹下的图片并且分类放到新创建的训练文件夹和标签文件夹
在深度学习的训练时,经常会碰到训练的样本数据集和标签数据集是在一个文件夹中,这个时候我们就不得不进行一些数据的预处理和文件的分类,例如将训练(training data)数据集和标签数据集(label ...
- 使用python删除指定文件夹及子文件,保留多少
python版本为:2.7 import os,time,shutil,datetime def rmdir(deldir,N): dellist=os.listdir(deldir) deldate ...
- 使用python删除一个文件或文件夹
使用python删除一个文件或文件夹,需要使用os模块. import osos.remove(path) # path是文件的路径,如果这个路径是一个文件夹,则会抛出OSError的错误,这时需用用 ...
- python 递归删除空文件夹
Python如何递归删除空文件夹 1.Python如何递归删除空文件夹,这个问题很常见.但大多数人的解决办法都是自己实现递归函数解决这个问题,其实根本不用那么麻烦.Python中的os.walk提供了 ...
- 两个简单的python文件,实现删除本地文件夹和mongodb数据库的内容
删除本地文件夹: import os , string , datetime ; str = '/home/niuguoqin/tmp/tomcat/'; b = (datetime.datetime ...
- Win7如何删除需要管理员权限才能删除的文件夹
在Windows 7系统运行中.往往会遇到想要删除某个文件夹时,系统提示:文件夹访问被拒绝 你需要权限来执行此操作,如何才能删除此类文件夹呢? ------------------ --------- ...
- PHP批量清空删除指定文件夹内容
PHP批量清空删除指定文件夹内容: cleancache.php <?php // 清文件缓存 $dirs = array( realpath(dirname(__FILE__) . '/../ ...
- gulp使用技巧-删除node_modules文件夹,解决目录层次太深删除报错的问题
问题描述: 在使用gulp当中,自动生成的node_modules文件夹,因为文件目录层级太深,无法系统删除,用360粉碎工具也报错 解决方法: 使用npm中的插件rimraf,专门用于删除的模块插件 ...
- 重装系统后删除Cygwin文件夹
1.右键点要删除Cygwin 文件夹,依次选属性-安全-高级-所有者-编辑,将所有者改为你的登录帐户,勾选下方“替换子容器和对象的所有者”. 2.在 属性-安全-高级对话框中选 权限选项卡,点更改权限 ...
随机推荐
- GROUP函数
GROUP_ID 首先我们看看官方的解释: 大意是GROUP_ID用于区分相同分组标准的分组统计结果. 解释起来比较抽象,下面我们来看看具体的案例. 例1:单一分组 SQL> select gr ...
- Python之游戏开发-飞机大战
Python之游戏开发-飞机大战 想要代码文件,可以加我微信:nickchen121 #!/usr/bin/env python # coding: utf-8 import pygame impor ...
- Java线上应用故障排查
线上故障主要2种: CPU利用率很高, 内存占用率很大 一.CPU利用率很高 1. top查询那个进程CPU使用率高 2. 显示进程列表 ps -mp pid -o THREAD,tid,time 找 ...
- Inspector's Dilemma(欧拉通路)
In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directio ...
- HttpURLConnection绕过HTTPS的SSL验证
(这个jdk环境需要是1.8,以上). 直接在类里面加一个static代码块 static { try { trustAllHttpsCertificates(); HttpsURLConnectio ...
- Thawte SSL Web Server 多域型SSL证书
Thawte SSL Web Server 多域型SSL证书,最多支持25个域名,需要验证域名所有权和申请单位信息,属于企业验证型SSL证书,提供40位/56位/128位,最高支持256位自适应加密. ...
- jmeter接口测试实战简例
1.接口需求文档说明 2.打开jmeter,新建线程组,线程组里面默认配置就可以 3.建立http请求,我这里是http请求,所以我选择这个,跟进实际情况 4.post请求说明,post直接加参数和参 ...
- [luoguP1388] 算式(DP)
传送门 看这个n<=15本以为是个状压DP 还是too young 这个题最神奇的地方是加括号是根据贪心的策略. 发现只有在一连串的加号两边加上括号才是最优的(想一想,为什么?) f[i][j] ...
- [luoguP1272] 重建道路
传送门 奇奇怪怪的分组背包. #include <cstdio> #include <cstring> #include <iostream> #define N ...
- noip模拟赛 无题
分析:这道题和以前做过的模拟赛题很像:传送门. 对于前30%的数据可以直接暴力求,k=1的数据利用线段树求区间最大值,没有修改操作可以用主席树.100%的数据主席树是肯定用不了的,观察到K非常小,可以 ...