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

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

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

  例如:有文件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-----删除同一文件夹下相似的图片的更多相关文章

  1. Python读取不同文件夹下的图片并且分类放到新创建的训练文件夹和标签文件夹

    在深度学习的训练时,经常会碰到训练的样本数据集和标签数据集是在一个文件夹中,这个时候我们就不得不进行一些数据的预处理和文件的分类,例如将训练(training data)数据集和标签数据集(label ...

  2. 使用python删除指定文件夹及子文件,保留多少

    python版本为:2.7 import os,time,shutil,datetime def rmdir(deldir,N): dellist=os.listdir(deldir) deldate ...

  3. 使用python删除一个文件或文件夹

    使用python删除一个文件或文件夹,需要使用os模块. import osos.remove(path) # path是文件的路径,如果这个路径是一个文件夹,则会抛出OSError的错误,这时需用用 ...

  4. python 递归删除空文件夹

    Python如何递归删除空文件夹 1.Python如何递归删除空文件夹,这个问题很常见.但大多数人的解决办法都是自己实现递归函数解决这个问题,其实根本不用那么麻烦.Python中的os.walk提供了 ...

  5. 两个简单的python文件,实现删除本地文件夹和mongodb数据库的内容

    删除本地文件夹: import os , string , datetime ; str = '/home/niuguoqin/tmp/tomcat/'; b = (datetime.datetime ...

  6. Win7如何删除需要管理员权限才能删除的文件夹

    在Windows 7系统运行中.往往会遇到想要删除某个文件夹时,系统提示:文件夹访问被拒绝 你需要权限来执行此操作,如何才能删除此类文件夹呢? ------------------ --------- ...

  7. PHP批量清空删除指定文件夹内容

    PHP批量清空删除指定文件夹内容: cleancache.php <?php // 清文件缓存 $dirs = array( realpath(dirname(__FILE__) . '/../ ...

  8. gulp使用技巧-删除node_modules文件夹,解决目录层次太深删除报错的问题

    问题描述: 在使用gulp当中,自动生成的node_modules文件夹,因为文件目录层级太深,无法系统删除,用360粉碎工具也报错 解决方法: 使用npm中的插件rimraf,专门用于删除的模块插件 ...

  9. 重装系统后删除Cygwin文件夹

    1.右键点要删除Cygwin 文件夹,依次选属性-安全-高级-所有者-编辑,将所有者改为你的登录帐户,勾选下方“替换子容器和对象的所有者”. 2.在 属性-安全-高级对话框中选 权限选项卡,点更改权限 ...

随机推荐

  1. configparser logging

    configparser模块 # 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). import configpar ...

  2. js 循环 js创建数组

    循环 for (var i = 0; i < myArray.length; i++) { console.log(myArray[i]); }; for (var arr in myArray ...

  3. Symmetry

    Description The figure shown on the left is left-right symmetric as it is possible to fold the sheet ...

  4. (转载)O(N)的素数筛选法和欧拉函数

    转自:http://blog.csdn.net/dream_you_to_life/article/details/43883367 作者:Sky丶Memory 1.一个数是否为质数的判定. 质数,只 ...

  5. Mac os安装MySQL数据库,系统提示mysql: command not found该怎么办

    当我们安装好MySQL后,在终端输入mysql命令,发现并不能看到自己安装的数据库,这是因为你没有配置环境变量. 在os系统中安装MySQL数据库默认保存在/usr/local/mysql 那么我们应 ...

  6. python之更加抽象 2014-4-6

    #更加抽象 12:50pm- 14:50 p112- 1.对象的魔力 多态 如count 在多种数据类型中都可以实现计数的功能 封装 对全局作用域中其他区域隐藏多余信息的原则 继承2.类和类型 创建类 ...

  7. xtu read problem training 4 A - Moving Tables

    Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...

  8. ZOJ3953 ZJU2017校赛(贪心)

    题意:给出n个区间,求至少删掉多少个区间使得不存在区间a, b, c 两两相交    (定义两个区间相交是,区间[l1, r1]和区间[l2, r2]相交,当且仅当存在一个数x,l1<=x< ...

  9. JSTL-SQL标签库

    主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 本章的前提需要先新建数据表及添加默认数据,脚本如下: -- -- 数据库: `test` ...

  10. 程序猿是如何解决SQLServer占CPU100%的--马非码

    http://www.cnblogs.com/marvin/p/ASolutionForSQLServerCauseHighCPU.html