色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

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

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

前言

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

注:第一種方法只對于連續圖片(例一個視頻里截下的圖片)準確率也較高,其效率高;第二種方法準確率高,但效率低

方法一:相鄰兩個文件比較相似度,相似就把第二個加到新列表里,然后進行新列表去重,統一刪除。

例如:有文件1-10,首先1和2相比較,若相似,則把2加入到新列表里,再接著2和3相比較,若不相似,則繼續進行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)

方法二:逐個去比較,若相似,則從原來列表刪除,添加到新列表里,若不相似,則繼續

例如:有文件1-10,首先1和2相比較,若相似,則把2在原列表刪除同時加入到新列表里,再接著1和3相比較,若不相似,則繼續進行1和4比較…一直比,到最后一個,再繼續,正常應該再從2開始比較,但2被刪除了,所以從3開始,繼續之前的操作,最后把新列表里的刪除。

代碼如下:

#!/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)

總結

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产一区免费观看 | 日本www色视频成人免费网站 | 国产亚洲视频在线播放大全 | 黄色三级网站免费 | 中文字幕1区 | 国产精品国产三级国产普通 | 欧美做暖小视频xo免费 | 91亚洲精品在看在线观看高清 | 亚洲精品国自产拍在线观看 | 亚洲欧美国产高清va在线播放 | 国产高清在线观看视频手机版 | 中文字幕亚洲综合久久 | 成人区精品一区二区不卡亚洲 | 国产亚洲精品精品国产亚洲综合 | 这里只有精品国产 | 18免费视频 | 国产亚洲精品久久久久久久 | 国产一级免费视频 | 女人张开腿让男人桶免费网站 | 日韩欧美亚洲中字幕在线播放 | 国产日韩欧美精品在线 | 久久女同互慰一区二区三区 | 亚洲第一页在线播放 | 亚洲网站在线观看 | 九九热视频精品 | 亚洲日韩aⅴ在线视频 | www.乱| 日韩欧美亚洲视频 | 91精品成人福利在线播放 | 欧美日本一道道一区二区三 | 美国一级毛片免费看成人 | 欧美一级精品 | 亚洲精品天堂一区 | 久久精品.com | 久久香蕉国产精品一区二区三 | 欧美一欧美一级毛片 | 日韩在线欧美在线 | 黄色在线视屏 | 老少配性xxxxxx | 国产主播福利片在线观看 | 欧美成人免费午夜全 |