将两个文件夹中重复的图象删除
- 需求分析
- 解决方案
需求分析
文件夹one和two存在图象的重复,将two文件中中重复的文件夹删除
解决方案
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json
#删除重名图象文件
def read_json(file_name):
with open(file_name, 'rb') as f:
data = json.load(f)
return data
#读取源图象的路径
def delete1(image_root_path,suffix):
delImg = []
for root, dirs, files in os.walk(image_root_path):
# 使用os模块获取文件夹中所有文件的路径
all_files = os.listdir(root)
filtered_files = [file for file in all_files if file.endswith(suffix)]
if (len(filtered_files)):
# print(root) #当前工作文件夹
for i in range(len(filtered_files)):
bmp_path = root + "\\" + filtered_files[i]
delImg.append(filtered_files[i])
return delImg
#在目标文件中删除图象
def delete2(image_root_path,suffix,delImg):
del_delImg = []
for root, dirs, files in os.walk(image_root_path):
# 使用os模块获取文件夹中所有文件的路径
all_files = os.listdir(root)
filtered_files = [file for file in all_files if file.endswith(suffix)]
if (len(filtered_files)):
# print(root) #当前工作文件夹
for i in range(len(filtered_files)):
bmp_path = root + "\\" + filtered_files[i]
for j in range(len(delImg)):
if(delImg[j]==filtered_files[i]):
print(bmp_path)
del_delImg.append(bmp_path)
return del_delImg
def mydelImg(del_delImg):
if(len(del_delImg)>0):
for i in range(len(del_delImg)):
bmp_path = del_delImg[i]
os.remove(bmp_path)
else:
print("无文件")
if __name__ == "__main__":
delImg = delete1("D:\\code\\select\\delete2\\one",".bmp")
del_delImg = delete2("D:\\code\\select\\delete2\\two",".bmp",delImg)
mydelImg(del_delImg)
完成