按照xml文件和txt文件名,将对应名字的图象文件复制
- 需求分析
- 解决方案
需求分析
就是已经标注了xml和txt文件,需要将其对应的图象文件挑选出来
解决方案
# 按照xml文件删除对应的图片
# 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 myCopyImg(del_delImg,desPath):
if(len(del_delImg)>0):
for i in range(len(del_delImg)):
bmp_path = del_delImg[i]
#os.remove(bmp_path)
shutil.copy(bmp_path, desPath) # shutil.copy函数放入原文件的路径文件全名 然后放入目标文件夹
else:
print("无文件")
#替换列表中文件的后缀
def tihuanhouozhui(delImg):
old_suffix1 = '.txt'
old_suffix2 = '.xml'
new_suffix = '.bmp'
delImg = [file.replace(old_suffix1, new_suffix) for file in delImg]
delImg = [file.replace(old_suffix2, new_suffix) for file in delImg]
return delImg
if __name__ == "__main__":
#my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")
delImg1 = delete1("E:\\黄花标注\\glass\\testtt\\yesann",".txt")
delImg2 = delete1("E:\\黄花标注\\glass\\testtt\\yesann", ".xml")
delImg = delImg1 + delImg2
new_delImg = tihuanhouozhui(delImg)
# for i in range(len(delImg)):
# os.remove(delImg[i])
del_delImg = delete2("E:\\黄花标注\\glass\\testtt\\yes",".bmp",new_delImg)
desPath = "E:\\黄花标注\\glass\\testtt\\yess"
myCopyImg(del_delImg,desPath)
完美解决
这里列表名字可能存在歧义,起始代码我是复制按照文件名字删除重名文件的
对于列表后缀的替换
old_suffix = '.txt'
new_suffix = '.csv'
my_list = ['file1.txt', 'file2.txt', 'file3.txt']
new_list = [file.replace(old_suffix, new_suffix) for file in my_list]
print(new_list)
将两个文件夹中重复的图象删除