av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

使用python如何刪除同一文件夾下相似的圖片

瀏覽:4日期:2022-06-20 13:52:24

前言

最近整理圖片發(fā)現(xiàn),好多圖片都非常相似,于是寫如下代碼去刪除,有兩種方法:

注:第一種方法只對(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 osimport cv2from 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_pictestrec_pic’ # save_path_img = r’E:0115_testrec_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]) 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 += 1if 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)?開始比較,但2被刪除了,所以從3開始,繼續(xù)之前的操作,最后把新列表里的刪除。

代碼如下:

#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport cv2from skimage.measure import compare_ssimimport shutilimport datetimedef 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:tempdemo’ # save_path_img = r’F:tempdemo_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_pathimgs_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_curif currIndex1 >= len(img_files) - currIndex - 1: breakelse: 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 in del_list: # yidong(image, save_path_img) delete(image) print(’delete’,image)

總結(jié)

到此這篇關(guān)于使用python如何刪除同一文件夾下相似圖片的文章就介紹到這了,更多相關(guān)python刪除文件夾相似圖片內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产香蕉视频 | 日韩精品一区二区三区免费观看 | 日本色高清 | 午夜精品一区二区三区在线视频 | 福利网址| 久久大| 草樱av| 欧美视频在线播放 | 在线播放国产一区二区三区 | 91国产精品| 亚洲成人自拍 | 精国产品一区二区三区四季综 | 久久久久免费精品国产小说色大师 | av天空| 日韩国产一区 | 久久久综合网 | 成人免费一区二区三区视频网站 | 亚洲一区二区三区高清 | av片免费 | 草久久 | 成人国产一区二区三区精品麻豆 | 在线亚洲一区二区 | 亚洲狠狠丁香婷婷综合久久久 | 国产成人精品一区二区三区视频 | 国产一在线 | 亚洲精品欧美 | 91精品国产一二三 | av国产在线观看 | 一区二区高清 | 精品一区二区三区在线观看 | 国产精品久久久久久久久久久久久 | 午夜成人在线视频 | 国产精品18hdxxxⅹ在线 | 请别相信他免费喜剧电影在线观看 | 久久久久国产一区二区三区 | 亚欧洲精品在线视频免费观看 | 亚洲精品欧美 | 亚洲精品9999久久久久 | 日本成人在线网址 | 国产精品久久久久久久久久久久午夜片 | 99免费在线观看 |