python을 사용하여 같은 폴더에서 비슷한 그림을 삭제하는 방법

5736 단어 python삭제그림
앞말
최근에 사진을 정리한 결과 많은 그림이 매우 비슷하다는 것을 발견하여 다음과 같은 코드를 써서 삭제하는 두 가지 방법이 있다.
주: 첫 번째 방법은 연속 이미지(예를 들어 한 영상에서 캡처한 이미지)에 대한 정확도도 높고 효율이 높다.두 번째 방법은 정확도는 높지만 효율은 낮다
방법1: 인접한 두 파일의 유사도를 비교하고 비슷하면 두 번째를 새 목록에 추가한 다음에 새 목록을 다시 만들고 통일적으로 삭제한다.
예를 들어 파일 1-10이 있으면 먼저 1과 2를 비교하고, 비슷하면 2를 새 목록에 추가하고, 이어서 2와 3을 비교하고, 비슷하지 않으면 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)
방법2: 하나하나 비교하여 비슷하면 원래 목록에서 삭제하고 새 목록에 추가하고 비슷하지 않으면 계속
예를 들어 파일 1-10이 있으면 먼저 1과 2를 비교하고 비슷하면 2를 원래 목록에서 삭제하고 새 목록에 추가한 다음에 1과 3을 비교하고 비슷하지 않으면 1과 4를 비교한다... 계속 비교하고 마지막까지 계속하고 정상적으로 2부터 비교하지만 2가 삭제되기 때문에 3부터 시작하고 이전의 조작을 계속하고 마지막으로 새 목록에서 삭제한다.
코드는 다음과 같습니다.

#!/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을 사용하여 같은 폴더 아래의 비슷한 그림을 어떻게 삭제하는지에 관한 글을 소개합니다. 더 많은 관련python이 폴더의 비슷한 그림을 삭제하는 내용은 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기