溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-05-07 14:07:39 來源:億速云 閱讀:257 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下怎么使用python刪除同一文件夾下相似的圖片,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

python有哪些常用庫

python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

前言

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

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

方法一:相鄰兩個文件比較相似度,相似就把第二個加到新列表里,然后進(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)

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

例如:有文件1-10,首先1和2相比較,若相似,則把2在原列表刪除同時加入到新列表里,再接著1和3相比較,若不相似,則繼續(xù)進(jìn)行1和4比較…一直比,到最后一個,再繼續(xù),正常應(yīng)該再從2開始比較,但2被刪除了,所以從3開始,繼續(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)

以上是“怎么使用python刪除同一文件夾下相似的圖片”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI