使用python如何刪除同一文件夾下相似的圖片
前言
最近整理圖片發(fā)現(xiàn),好多圖片都非常相似,于是寫(xiě)如下代碼去刪除,有兩種方法:
注:第一種方法只對(duì)于連續(xù)圖片(例一個(gè)視頻里截下的圖片)準(zhǔn)確率也較高,其效率高;第二種方法準(zhǔn)確率高,但效率低
方法一:相鄰兩個(gè)文件比較相似度,相似就把第二個(gè)加到新列表里,然后進(jìn)行新列表去重,統(tǒng)一刪除。
例如:有文件1-10,首先1和2相比較,若相似,則把2加入到新列表里,再接著2和3相比較,若不相似,則繼續(xù)進(jìn)行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)
方法二:逐個(gè)去比較,若相似,則從原來(lái)列表刪除,添加到新列表里,若不相似,則繼續(xù)
例如:有文件1-10,首先1和2相比較,若相似,則把2在原列表刪除同時(shí)加入到新列表里,再接著1和3相比較,若不相似,則繼續(xù)進(jìn)行1和4比較…一直比,到最后一個(gè),再繼續(xù),正常應(yīng)該再?gòu)?開(kāi)始比較,但2被刪除了,所以從3開(kāi)始,繼續(xù)之前的操作,最后把新列表里的刪除。
代碼如下:
#!/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)
總結(jié)
到此這篇關(guān)于使用python如何刪除同一文件夾下相似圖片的文章就介紹到這了,更多相關(guān)python刪除文件夾相似圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)繪制3D地球旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)繪制出3D地球旋轉(zhuǎn)的效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02Django數(shù)據(jù)庫(kù)遷移報(bào)錯(cuò)InconsistentMigrationHistory
最近在使用Django,學(xué)習(xí)了一下Django數(shù)據(jù)庫(kù)遷移,在執(zhí)行遷移命令時(shí),突然報(bào)錯(cuò),本文就總結(jié)了一下原因,感興趣的小伙伴們可以參考一下2021-05-05python實(shí)現(xiàn)加密的方式總結(jié)
這篇文章主要介紹了python實(shí)現(xiàn)加密的方式總結(jié),文中給大家提到了python中加密的注意點(diǎn),通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實(shí)現(xiàn)
這篇文章主要介紹了pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Python多線程threading join和守護(hù)線程setDeamon原理詳解
這篇文章主要介紹了Python多線程threading join和守護(hù)線程setDeamon原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python中xml.etree.ElementTree的使用示例
ElementTree是Python標(biāo)準(zhǔn)庫(kù)中的一個(gè)模塊,專門(mén)用于處理XML文件,它提供了解析、創(chuàng)建、修改和遍歷XML文檔的API,非常適合處理配置文件、數(shù)據(jù)交換格式和Web服務(wù)響應(yīng)等場(chǎng)景,本文就來(lái)介紹一下,感興趣的可以了解一下2024-09-09詳解Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用
這篇文章主要介紹了Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用,觀察者模式和策略模式都可以歸類為結(jié)構(gòu)型的設(shè)計(jì)模式,需要的朋友可以參考下2016-03-03