赞
踩
from PIL import Image import os import os.path import numpy as np import glob import pandas as pd import xml.etree.ElementTree as ET import time start = time.time() n = 0 # 指明被遍历的文件夹 rootdir = r'D:/数据集/XXXX/0bag_color/' for parent, dirnames, filenames in os.walk(rootdir): # 遍历每一张图片 for filename in filenames: # print('parent is :' + parent) # print('filename is :' + filename) currentPath = os.path.join(parent, filename) # 目前图片的地址 picture_number = filename[:-4] #图片去掉.png后的名字 print("the picture name is:" + filename) # print('the full name of the file is :' + currentPath) # 读取图片 # print (img.format, img.size, img.mode) # img.show() xml_path = r'D:/数据集/XXXX/xml/' #xml文件存放的文件夹地址 xml_file = os.listdir(xml_path) #将xml文件存放在xml_file列表里面 if picture_number + '.xml' in xml_file: #如果图片有对应的xml文件则跳过 pass else: print(currentPath) n += 1 os.remove(currentPath) #没有对应的xml文件则删除 end = time.time() print("Execution Time:", end - start) print("删除的照片数为:" ,n)
from PIL import Image import os import os.path import numpy as np import glob import pandas as pd import xml.etree.ElementTree as ET import time import shutil def mymovefile(srcfile, dstfile): if not os.path.isfile(srcfile): print("%s not exist!" % (srcfile)) else: fpath, fname = os.path.split(dstfile) # 分离文件名和路径 if not os.path.exists(fpath): os.makedirs(fpath) # 创建路径 # 移动文件 print("move %s -> %s" % (srcfile, dstfile)) start = time.time() n = 0 # 指明被遍历的文件夹,文件夹下可以有很多文件夹,这个遍历会找文件夹下的picture文件夹。图片都存放在文件夹“picture”下,xml文件存放在文件夹“xml”下 rootdir = r'D:/数据集/灰机/' for parent, dirnames, filenames in os.walk(rootdir): for dirname in dirnames: print(dirname) if dirname == "picture": 先找到名字为picture的文件夹 currentPath = os.path.join(parent, dirname) print(currentPath) if not os.path.isdir(os.path.join(parent, "move")): os.mkdir(os.path.join(parent, "move")) #没有move文件夹则创建move文件夹 for root, dirs, files in os.walk(currentPath): #找到picture文件夹的父目录,即currentPath后,遍历picture内的图片 for file in files: # print('parent is :' + parent) # print('filename is :' + filename) picturePath = os.path.join(root,file) # 目前图片的地址 picture_number = file[:-4] print("the picture name is:" + file) print('the full name of the file is :' + picturePath) xml_path = os.path.join(parent, "xml") xml_file = os.listdir(xml_path) if picture_number + '.xml' in xml_file: pass else: n += 1 srcfile = os.path.join(currentPath, file) dstfile = os.path.join(parent, "move",file) shutil.move(srcfile, dstfile) //把图片从picture内移动到picture文件夹父文件夹下的“move”文件夹 end = time.time() print("Execution Time:", end - start) print("移动的照片数为:", n)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。