-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdec.py
More file actions
76 lines (60 loc) · 2.03 KB
/
dec.py
File metadata and controls
76 lines (60 loc) · 2.03 KB
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
import os
from PIL import Image
from mclib_renderer import mclibRenderer
from pvr_ccz_decoder import pvr_ccz_decode
from tp_atlas_parser import atlas_to_sprites
class InvalidCCZException(Exception):
pass
def read(path):
with open(path, "rb") as f:
data = f.read()
f.close()
return data
def process_image(folder, name, folder_out, logging=False):
master_path = os.path.join(folder, f"{name}.png")
alpha_path = os.path.join(folder, f"{name}.png@alpha")
atlas_path = os.path.join(folder, f"{name}.plist")
mclib_path = os.path.join(folder, f"{name.replace('image', '')}.mclib")
out = os.path.join(folder_out, f"{name}.png")
if not os.path.exists(master_path):
raise Exception("missing master file !")
has_alpha = os.path.exists(alpha_path)
is_atlas = os.path.exists(atlas_path)
has_animation = os.path.exists(mclib_path)
logging and print(f"[{name}] has alpha : {has_alpha} | is atlas : {is_atlas} | has animation : {has_animation}")
master_data = read(master_path)
try:
master = pvr_ccz_decode(master_data, logging=logging, exc=InvalidCCZException)
except InvalidCCZException:
print(f"[{name}] invalid ccz exception")
Image.open(master_path).save(out)
return
except Exception as e:
print(f"[{name}] {e}")
return
if has_alpha:
alpha_data = read(alpha_path)
alpha = pvr_ccz_decode(alpha_data, logging=logging)
# alpha.save("alpha.png")
alpha = alpha.convert("L")
master = master.convert("RGBA")
r, g, b, _ = master.split()
master = Image.merge("RGBA", (r, g, b, alpha))
# done
os.makedirs(os.path.dirname(out), exist_ok=True)
master.save(out)
# additionnal step
doSprites = False
sprites = None
if is_atlas and doSprites:
atlas = read(atlas_path)
sprites = atlas_to_sprites(master, atlas, logging)
for k, v in sprites.items():
sprite_out = os.path.join(folder_out, name, f"{k}")
os.makedirs(os.path.dirname(sprite_out), exist_ok=True)
v.save(sprite_out)
# # another
# if has_animation:
# mclib = read(mclib_path)
# test = mclibRenderer(mclib, sprites)
# # render_animations(mclib, sprites)