Skip to content

Instantly share code, notes, and snippets.

@savan77
Created October 20, 2020 21:49
Show Gist options
  • Select an option

  • Save savan77/9cdbdf94c3942cacf35b3738b9965310 to your computer and use it in GitHub Desktop.

Select an option

Save savan77/9cdbdf94c3942cacf35b3738b9965310 to your computer and use it in GitHub Desktop.
Crop license plates
def get_crops(args):
tree = ET.parse(args.xml_file)
root = tree.getroot()
count = 0
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
for image in root.iter('image'):
img = Image.open(os.path.join(args.image_dir, image.attrib['name']))
for idx, box in enumerate(image.findall('box')):
cropped = img.crop((float(box.attrib['xtl']), float(box.attrib['ytl']), float(box.attrib['xbr']), float(box.attrib['ybr'])))
cropped.save(os.path.join(args.output_dir, image.attrib['name'][:-4]+'_'+str(idx)+image.attrib['name'][-4:]))
_ = open(os.path.join(args.output_dir, image.attrib['name'][:-4]+'_'+str(idx)+'.txt'), 'w') #empty txt file to be filled with annotations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment