通过xml复制满足条件的图片到指定文件夹

import os 
import shutil
import xml.etree.ElementTree as ET
# xml文件夹

def image_copy(root,save_dir,img_dir):
    for image in root.findall('image'):
        img_name = image.attrib.get('name')
        for x in image.findall('tag'):
            image_label = x.attrib.get('label')
        save_path1 = os.path.join(save_dir,image_label)
        os.makedirs(save_path1,exist_ok=True)
        save_path2 = os.path.join(save_path1,img_name)
        img_path = os.path.join(img_dir,img_name)
        shutil.copyfile(img_path, save_path2)

if __name__ == '__main__':
    xml_path = ''
    save_dir = ''
    img_dir = ''
    tree = ET.parse(xml_path)
    root = tree.getroot()
    image_copy(root,save_dir,img_dir) 

猜你喜欢

转载自blog.csdn.net/a1004550653/article/details/128677800