python删除文件夹中的jpg_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

importosimportcv2from skimage.measure importcompare_ssim#import shutil#def yidong(filename1,filename2):#shutil.move(filename1,filename2)

defdelete(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 inenumerate(img_files):if notos.path.exists(img_files[currIndex]):print('not exist', img_files[currIndex])breakimg=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 inimgs_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

importosimportcv2from skimage.measure importcompare_ssimimportshutilimportdatetimedefyidong(filename1,filename2):

shutil.move(filename1,filename2)defdelete(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) inos.walk(path):for dirc indirs: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 inenumerate(img_files):if notos.path.exists(img_files[currIndex]):print('not exist', img_files[currIndex])breaknew_cur=0for i in range(10000000):

currIndex1=new_curif 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=currIndex1else:

new_cur= currIndex1 + 1

print('small_ssim',img_files[currIndex], img_files[currIndex1 + currIndex + 1], ssim)for image indel_list:#yidong(image, save_path_img)

delete(image)print('delete',image)

如果有更好的方法,欢迎留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值