**
制作超分辨率数据集
**
说明:(1) 有图片的xml标注文件。目录格式:Annotations–>类别文件夹–>1.xml
(2) 有图片文件。目录格式:JPEGImages–>JPEGImages–>1.jpg
难点:(1)读取文件夹内文件名字,可获得该文件夹内所有文件名称:file_name = os.listdir(xml_path)
(2)读取xml文件,详细看代码
(3)图片分片,从原图中截取固定大小图片。cv2:当文件目录中存在中文不能使用。应使用PIL,具体看代码
(4)保存图片,可通过quility参数类控制保存图片质量。也可将图片保存为.png格式。
(5)建立文件夹时,判断该文件夹是否存在,if not isExists: os.makedirs(os.path.join(save_path,cls))
import sys
import os
from xml.etree import ElementTree as ET
#import cv2
from PIL import Image
Img_path = r'E:\JPEGImages\JPEGImages' #图片保存目录
save_path = r'E:\image_local' #可视化后图片保存目录
xml_path=r'E:\Annotations' #xml文件保存目录
def cut(file,file_cls):
imgname = file_cls.replace('.xml', '.jpg')
print (imgname)
#print (file)
file_id = os.path.join(file, file_cls )
tree = ET.parse(os.path.join(xml_path,file_id))
for obj in tree.iter('object'):
bbox = obj.find('bndbox')
# Make pixel indexes 0-based
x1 = int(bbox.find('xmin').text)
y1 = int(bbox.find('ymin').text)
x2 = int(bbox.find('xmax').text)
y2 = int(bbox.find('ymax').text)
cls = obj.find('name').text.strip()
img = Image.open(os.path.join(Img_path,file,imgname))
#print(img)
cropped = img.crop((x1,y1,x2,y2)) #切片 用来街截取图片
imagename=cls+imgname
isExists=os.path.exists(os.path.join(save_path,cls))
if not isExists:
os.makedirs(os.path.join(save_path,cls))
x,y=cropped.size
if x >=300 or y >=300 :
cropped.save(os.path.join(save_path,cls,imagename),quality=95) #保存图片没有损失
if __name__=='__main__':
b = 0
file_name = os.listdir(xml_path)
for file in file_name:
for file_cls in os.listdir(os.path.join(xml_path,file)):
print (file_cls)
cut(file,file_cls)
b = b + 1
print(b)