-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.py
74 lines (55 loc) · 1.59 KB
/
highlighter.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import xml.etree.ElementTree as ET
from PIL import Image, ImageDraw
import glob
def main():
#create a list of all xml files in this folder
files = glob.glob('*.xml')
for xml_file in files:
#account for broken xml files
try:
tree = ET.parse(xml_file)
except:
print("Error parsing, skipping file: "+ xml_file +" ")
continue
#grab matching png file
png_file = xml_file[:-4]+'.png'
#setup for finding leaf components
root = tree.getroot()
leafs = []
#Recursively search through the tree for components with no children
def recursive(root):
root_len = len(root)
if (root_len<1):
#found a leaf component
leafs.append(root)
return
for x in range(root_len):
temp = root[x]
recursive(temp)
recursive(root)
#draw a box around the bounds of each leaf components
try:
image = Image.open(png_file)
except:
print("Couldn't find matching png, skipping file: "+xml_file)
continue
draw = ImageDraw.Draw(image)
for x in leafs:
#Get the bounds of the component
bounds = x.get('bounds')
#clean it up
bounds = bounds.replace('[', '')
bounds = bounds.replace(']', ' ')
bounds = bounds.replace(',', ' ')
barr = bounds.split()
#parse out coordinates
x1 = int(barr[0])
x2 = int(barr[2])
y1 = int(barr[1])
y2 = int(barr[3])
draw.rectangle((x1, y1, x2, y2), width = 5, outline = "yellow")
#save the final image
image.save(png_file[:-4]+"_result.png")
#show successful completion
print("Results saved to folder")
main()