This repository has been archived by the owner on Feb 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
draw_image.py
89 lines (79 loc) · 3.39 KB
/
draw_image.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json, os
from settings import *
from PIL import Image
from multiprocessing.dummy import Pool
def draw(hier, img, FILE_NAME):
#
# Some non-standard layout/views, they don't have standard name.
# Therefore, we have to guess the label using their name and resource-id.
#
checked = False
for element_name in ELEMENT_COLOR.keys():
if hier['name'].count(element_name) > 0:
except_occur = False
for element_except in ELEMENT_EXCEPT:
if hier['resource_id'] is not None and hier['resource_id'].count(element_except) > 0:
except_occur = True
break
if not except_occur:
hier['name'] = element_name
checked = True
break
if not checked and hier['resource_id'] is not None:
for element_name in ELEMENT_COLOR.keys():
if hier['resource_id'].count(element_name) > 0:
except_occur = False
for element_except in ELEMENT_EXCEPT:
if hier['resource_id'].count(element_except) > 0:
except_occur = True
break
if not except_occur:
hier['name'] = element_name
break
#
# Some non-standard layout/views which can't be handled by name/resource-id,
# they should be processed using full_name(class information).
#
full_name_checked = False
for full_name in FULL_NAME_COLOR.keys():
if hier['full_name'].count(full_name) > 0 and hier['visible']:
full_name_checked = True
b = hier['bounds']
try:
block = Image.new('RGBA', (b[2] - b[0], b[3] - b[1]), FULL_NAME_COLOR[full_name])
img.paste(block, (b[0], b[1]))
if ((b[2] - b[0]) * (b[3] - b[1]))/(SCREEN_SIZE[0] * SCREEN_SIZE[1]) > 0.8:
print(FILE_NAME + ":" + full_name + ":Too large:" + str(((b[2] - b[0]) * (b[3] - b[1]))/(SCREEN_SIZE[0] * SCREEN_SIZE[1])))
except ValueError:
pass
# Coloring
if not full_name_checked and hier['name'] in ELEMENT_COLOR.keys() and hier['visible']:
b = hier['bounds']
try:
block = Image.new('RGBA', (b[2] - b[0], b[3] - b[1]), ELEMENT_COLOR[hier['name']])
img.paste(block, (b[0], b[1]))
if ((b[2] - b[0]) * (b[3] - b[1])) / (SCREEN_SIZE[0] * SCREEN_SIZE[1]) > 0.8:
print(FILE_NAME + ":" + hier['name'] + ":Too large:" + str(
((b[2] - b[0]) * (b[3] - b[1])) / (SCREEN_SIZE[0] * SCREEN_SIZE[1])))
except ValueError:
pass
# Recursion
if hier['children'] is not None:
for child in hier['children']:
draw(child, img, FILE_NAME)
def work(FILE_NAME):
with open(os.path.join(os.path.curdir, 'json', 'refined', FILE_NAME), mode='r') as file:
data = json.load(file)
img = Image.new('RGBA', SCREEN_SIZE, 'white')
try:
draw(data['hierarchy'], img, FILE_NAME)
img.save(os.path.join(os.path.curdir, 'layout', FILE_NAME + '_out.png'), 'PNG')
print(os.path.join(os.path.curdir, 'layout', FILE_NAME + '_out.png'))
except:
print("ERROR: " + os.path.join(os.path.curdir, 'layout', FILE_NAME + '_out.png'))
pass
# Multi-thread parts
threads = Pool(40)
threads.map(work, FILE_LIST)
threads.close()
threads.join()