forked from smiger/unpacker_texturemerger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpacker.py
86 lines (79 loc) · 2.77 KB
/
unpacker.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
#!/usr/bin/env python
import os
import sys
from PIL import Image
import json
def frames_from_data(filename, ext):
data_filename = filename + ext
if ext == '.json':
json_data = open(data_filename)
data = json.load(json_data)
frames = {}
for f in data['frames']:
x = int(float(data['frames'][f]["x"]))
y = int(float(data['frames'][f]["y"]))
w = int(float(data['frames'][f]["w"]))
h = int(float(data['frames'][f]["h"]))
real_w = int(float(data['frames'][f]["sourceW"]))
real_h = int(float(data['frames'][f]["sourceH"]))
d = {
'box': (
x,
y,
x + w,
y + h
),
'real_sizelist': [
real_w,
real_h
],
'result_box': (
int((real_w - w) / 2),
int((real_h - h) / 2),
int((real_w + w) / 2),
int((real_h + h) / 2)
),
'rotated': False
}
frames[f] = d
json_data.close()
return frames.items()
else:
print("Wrong data format on parsing: '" + ext + "'!")
exit(1)
def gen_png_from_data(filename, ext):
big_image = Image.open(filename + ".png")
frames = frames_from_data(filename, ext)
for k, v in frames:
frame = v
box = frame['box']
rect_on_big = big_image.crop(box)
real_sizelist = frame['real_sizelist']
result_image = Image.new('RGBA', real_sizelist, (0, 0, 0, 0))
result_box = frame['result_box']
result_image.paste(rect_on_big, result_box, mask=0)
if frame['rotated']:
result_image = result_image.transpose(Image.ROTATE_90)
if not os.path.isdir(filename):
os.mkdir(filename)
outfile = (filename + '/' + k)
if not outfile.endswith('.png'):
outfile += '.png'
print(outfile, "generated")
result_image.save(outfile)
def get_sources_file(filename):
data_filename = filename + ext
png_filename = filename + '.png'
if os.path.exists(data_filename) and os.path.exists(png_filename):
gen_png_from_data(filename, ext)
else:
print("Make sure you have both " + data_filename + " and " + png_filename + " files in the same directory")
# Use like this: python unpacker.py [Image Path or Image Name(but no suffix)] [Type:plist or json]
if __name__ == '__main__':
if len(sys.argv) <= 1:
print("You must pass filename as the first parameter!")
exit(1)
# filename = sys.argv[1]
path_or_name = sys.argv[1]
ext = '.json'
get_sources_file(path_or_name)