forked from ghtalpo/py_amazfit_tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
convert.py
117 lines (98 loc) · 3.86 KB
/
convert.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import logging
class Converter:
width_gtr = 454
width_verge_lite = 360
width_bip = 176
def __init__(self):
self.source_size = 0
self.target_size = Converter.width_verge_lite
def loadJson(self, json_path):
try:
import json
with open(json_path, 'rb') as stream:
return json.load(stream)
except Exception as e:
logging.fatal(e, exc_info=True)
return None
def saveJson(self, json_path, tree):
try:
import json
def dumper(obj):
try:
return obj.toJSON()
except AttributeError:
return obj.__dict__
with open(json_path, 'w') as stream:
stream.write(json.dumps(tree, default=dumper, indent = 2))
stream.flush()
except Exception as e:
logging.fatal(e, exc_info=True)
def patchTree(self, tree, depth = 0):
for k in tree:
value = tree[k]
if type(value) == int:
if k.endswith('X') or k.endswith('Y') or k == 'Spacing':
tree[k] = int(value / self.source_size * self.target_size)
elif type(value) == dict:
self.patchTree(value, depth + 1)
def resizeJson(self, json_path):
tree = self.loadJson(json_path)
# prevent PreviewStates.json from being parsed
# from (chenchix)
if not 'Background' in tree:
return
self.patchTree(tree)
self.saveJson(json_path, tree)
def detectRatio(self, top_dir):
target_path = os.path.join(top_dir, '0000.png')
from PIL import Image
im = Image.open(target_path)
(w, h) = im.size
if w == h:
if w == Converter.width_gtr:
self.source_size = Converter.width_gtr
print('GTR source')
return
elif w == Converter.width_bip:
self.source_size = Converter.width_bip
print('BIP source')
return
raise Exception(f"Unsupported image source dimension({w}/{h})")
def resizePng(self, top_dir):
from PIL import Image
self.detectRatio(top_dir)
for root, _, files in os.walk(top_dir, topdown=False):
for name in files:
full_path = os.path.join(root, name)
if name.endswith('.png'):
im = Image.open(full_path)
(w, h) = im.size
new_w = int(w * self.target_size / self.source_size)
new_h = int(h * self.target_size / self.source_size)
im = im.convert('RGBA')
im_resized = im.resize((new_w, new_h), resample = Image.LANCZOS)
im_resized.save(full_path)
elif name.endswith('.json'):
self.resizeJson(full_path)
print('Done...', top_dir)
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print("Usage example:")
print(" %s watchface.bin - unpacks watchface images and config" % (sys.argv[0]))
print(" %s watchface.json - packs config and referenced images to bin file" % (sys.argv[0]))
sys.exit(1)
args = sys.argv[1:]
if len(args) > 1:
print("Multitple files unpacking")
for inputFileName in args:
isDirectory = os.path.isdir(inputFileName)
isFile = os.path.isfile(inputFileName)
if not isDirectory and not isFile:
print("File or direcotry %s doesn't exists." % (inputFileName, ))
continue
if not isDirectory:
print("Not supported yet.")
sys.exit(1)
Converter().resizePng(inputFileName)