使用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


猜你喜欢
- 一个例子: print("Loading vgg19 weights...")vgg_mode
- 前言如何通过python实现邮件解析?邮件的格式十分复杂,主要是mime协议,本文主要是从实现出发,具体原理可以自行研究。一、安装通过mai
- 总结为:改注册表。顺手写个脚本:import tkinter as tkfrom tkinter import ttkimport winr
- step 1:perpar database & datause mastergoCreate database Inventory
- 本文实例讲述了Python OS模块。分享给大家供大家参考,具体如下:os模块在自动化测试中,经常需要查找操作文件,比如查找配置文件(从而读
- 方法一:引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的
- 准备本文环境信息:软件版本CentOSCentOS 7.4MySQL8.0.x安装前先更新系统所有包sudo yum update安装1.
- 本文实例讲述了Python数据结构与算法之完全树与最小堆。分享给大家供大家参考,具体如下:# 完全树 最小堆class CompleteTr
- 之前项目有一个需求,业务人员使用中文编写一些自定义公式,然后需要我们后台执行将结果返回到界面上,于是就基于有限状态机写了这个词法分析器,比较
- 合并在numpy中合并两个arraynumpy中可以通过concatenate,参数axis=0表示在垂直方向上合并两个数组,等价于np.v
- 安全公司 Imperva Cloud WAF 保护了全球超过10万个网站,并且每天观察到大约10亿次攻击。他们每天都会检测到成千上万种黑客工
- 1. 使用 fileinput 进行迭代fileinput 模块可以对一个或多个文件中的内容进行迭代、遍历等操作。该模块的 input()
- 以下就重复记录删除的问题作一阐述。 有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如N
- 早在02年,国外关于SQL注入漏洞的技术文章已经很多,而国内在05年左右才开始的。 如今,谈SQL注入漏洞是否已是明日黄花,国内大大小小的网
- MySQL Shell import_table数据导入1. import_table介绍这一期我们介绍一款高效的数据导入工具,MySQL
- 安装lxml首先需要pip install lxml安装lxml库。如果你在ubuntu上遇到了以下错误:#include "li
- 知道如何快速在命令行或者python脚本中实例化一个浏览器通常是非常有用的。每次我需要做任何关于web的自动任务时,我都使用这段python
- <P><html><head><meta http-equiv="Content-Typ
- 自从认识了 CircleCI 之后,基本上都在用这个了。相比于之前用的travis-ci ,CircleCI 丑是丑了点,但是相比与 tra
- 本文实例讲述了PHP依赖注入原理与用法。分享给大家供大家参考,具体如下:引言依然是来自到喜啦的一道面试题,你知道什么是依赖注入吗?依赖注入(