-
Notifications
You must be signed in to change notification settings - Fork 0
/
cropping_resize_images.py
48 lines (37 loc) · 1.33 KB
/
cropping_resize_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import xml.etree.ElementTree as ET
from PIL import Image
from pathlib import Path
import os, glob
dog_images = glob.glob('./images/*/*')
breeds = glob.glob('./annotations/*')
annotations = glob.glob('./annotations/*/*')
def get_bounding_boxes(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
bounding_boxes = []
for obj in root.findall('object'):
bbox = obj.find('bndbox')
xmin = int(bbox.find('xmin').text)
ymin = int(bbox.find('ymin').text)
xmax = int(bbox.find('xmax').text)
ymax = int(bbox.find('ymax').text)
bounding_boxes.append((xmin, ymin, xmax, ymax))
return bounding_boxes
def get_image(annot):
img_path = './images/'
file = annot.split('/')
img_filename = img_path + file[-2]+'/'+file[-1]+'.jpg'
return img_filename
for i in range(len(dog_images)):
bbox = get_bounding_boxes(annotations[i])
dog = get_image(annotations[i])
im = Image.open(dog)
for j in range(len(bbox)):
im2 = im.crop(bbox[j])
im2 = im2.resize((100,100), Image.ANTIALIAS)
new_path = dog.replace('./images/','./Cropped/')
new_path = new_path.replace('.jpg','-' + str(j) + '.jpg')
im2=im2.convert('RGB')
head, tail = os.path.split(new_path)
Path(head).mkdir(parents=True, exist_ok=True)
im2.save(new_path)