According to the xml file and txt file name, copy the image file with the corresponding name

According to the xml file and txt file name, copy the image file with the corresponding name

demand analysis

That is, the xml and txt files have been marked, and the corresponding image files need to be selected.

Insert image description here

Insert image description here

solution

# 按照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)


Insert image description here
Insert image description here

perfect solution

There may be ambiguity in the list names here. I copied the starting code to delete files with duplicate names according to the file names.Insert image description here

For list suffix replacement

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)

Delete duplicate images from two folders

Guess you like

Origin blog.csdn.net/qq_41701723/article/details/132738914