#coding:utf-8
from PIL import Image
import os
import glob
import xml.etree.ElementTree as ET
import xml.dom.minidom
import random
def changePicAndXmlName(oldImg, oldXml, newImg, newXml):
n = 1
if not os.path.exists(newImg):
os.mkdir(newImg)
if not os.path.exists(newXml):
os.mkdir(newXml)
xmlsList = os.listdir(oldXml)
random.shuffle(xmlsList)
for xmlfile in xmlsList:
xmlname = os.path.splitext(xmlfile)[0]
for pngfile in os.listdir(oldImg):
try:
pngname = os.path.splitext(pngfile)[0]
if pngname == xmlname:
if os.path.splitext(pngfile)[1] == '.png':
olddir = os.path.join(os.path.abspath(oldImg), pngname + ".png")
newdir = os.path.join(os.path.abspath(newImg), '%05d' % int(n)+".png")
elif os.path.splitext(pngfile)[1] == '.jpg':
olddir = os.path.join(os.path.abspath(oldImg), pngname + ".jpg")
newdir = os.path.join(os.path.abspath(newImg), '%05d' % int(n)+".jpg")
else:
olddir = os.path.join(os.path.abspath(oldImg), pngname + ".JPG")
newdir = os.path.join(os.path.abspath(newImg), '%05d' % int(n)+".JPG")
dom = xml.dom.minidom.parse(os.path.join(oldXml, xmlfile))
root = dom.documentElement
root.getElementsByTagName('filename')[0].firstChild.data = '%05d' % int(n) + os.path.splitext(pngfile)[1]
old_xmldir = os.path.join(oldXml, xmlfile)
new_xmldir = os.path.join(newXml, '%05d' % int(n)+'.xml')
with open(old_xmldir, 'w') as fh:
dom.writexml(fh)
os.rename(old_xmldir, new_xmldir)
os.rename(olddir, newdir)
n += 1
except Exception as e:
print(e)
continue
# os.rename(old_dir, new_dir)
print('total number is ', n)
def create_dir_not_exist(path):
if not os.path.exists(path):
os.mkdir(path)
if __name__ == "__main__":
allPath = '/home/hs/data/data_jiaxingyili/goods/smalldeal'
oldimgs = allPath + '/imgL/'
oldxmls = allPath + '/xmlL/'
newimgs = allPath + '/imgc/'
newxmls = allPath + '/xmlc/'
create_dir_not_exist(newimgs)
create_dir_not_exist(newxmls)
changePicAndXmlName(oldimgs, oldxmls, newimgs, newxmls)
pass
05-03
05-03
05-03
05-03
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交