python中删除相似图片的方法
这篇文章主要介绍python中删除相似图片的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
方法一:相邻两个文件比较相似度,相似就把第二个加到新列表里,然后进行新列表去重,统一删除。
例如:有文件1-10,首先1和2相比较,若相似,则把2加入到新列表里,再接着2和3相比较,若不相似,则继续进行3和4比较...一直比到最后,然后删除新列表里的图片
代码如下:
#!/usr/bin/envpython#-*-coding:utf-8-*-#@Time:2019/1/159:19#@Author:xiaodaiimportosimportcv2fromskimage.measureimportcompare_ssim#importshutil#defyidong(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=pathimgs_n=[]num=[]img_files=[os.path.join(rootdir,file)forrootdir,_,filesinos.walk(path)forfileinfilesif(file.endswith('.jpg'))]forcurrIndex,filenameinenumerate(img_files):ifnotos.path.exists(img_files[currIndex]):print('notexist',img_files[currIndex])breakimg=cv2.imread(img_files[currIndex])img1=cv2.imread(img_files[currIndex+1])ssim=compare_ssim(img,img1,multichannel=True)ifssim>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+=1ifcurrIndex>=len(img_files)-1:breakforimageinimgs_n:#yidong(image,save_path_img)delete(image)
方法二:逐个去比较,若相似,则从原来列表删除,添加到新列表里,若不相似,则继续
例如:有文件1-10,首先1和2相比较,若相似,则把2在原列表删除同时加入到新列表里,再接着1和3相比较,若不相似,则继续进行1和4比较...一直比,到最后一个,再继续,正常应该再从2开始比较,但2被删除了,所以从3开始,继续之前的操作,最后把新列表里的删除。
代码如下:
#!/usr/bin/envpython#-*-coding:utf-8-*-#@Time:2019/1/1612:03#@Author:xiaodaiimportosimportcv2fromskimage.measureimportcompare_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):fordircindirs:ifdirc=='rec_pic':pic_path=os.path.join(root,dirc)img_path=pic_pathimgs_n=[]num=[]del_list=[]img_files=[os.path.join(rootdir,file)forrootdir,_,filesinos.walk(img_path)forfileinfilesif(file.endswith('.jpg'))]forcurrIndex,filenameinenumerate(img_files):ifnotos.path.exists(img_files[currIndex]):print('notexist',img_files[currIndex])breaknew_cur=0foriinrange(10000000):currIndex1=new_curifcurrIndex1>=len(img_files)-currIndex-1:breakelse:size=os.path.getsize(img_files[currIndex1+currIndex+1])ifsize<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)ifssim>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+1print('small_ssim',img_files[currIndex],img_files[currIndex1+currIndex+1],ssim)forimageindel_list:#yidong(image,save_path_img)delete(image)print('delete',image)
以上是python中删除相似图片的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。