-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage2acp.py
86 lines (70 loc) · 2.09 KB
/
image2acp.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/python
import numpy as np
import os
import pathlib
import re
import sys
import subprocess
from PIL import Image
def convert2acp(filepath, size_option, ascii_option):
magick_exe = 'magick'
work_filename = 'work.gif'
print('Processing "%s"...' % filepath)
if size_option == 'keep':
resize_params = ''
else:
resize_params = '-resize %s -gravity center -background white -extent %s' % (size_option, size_option)
subprocess.run('%s %s %s -dither FloydSteinberg -remap palette.gif %s' % (magick_exe, filepath, resize_params, work_filename))
img = Image.open(work_filename)
img_pal = np.array(img.getpalette()).reshape(-1, 3)
master_pal = np.array([[0,0,0], [255,255,255], [0,128,0], [0,0,255], [255,0,0], [255,255,0], [255,170,0]])
index_map = list(range(7))
for src_index in range(7):
for target_index in range(7):
if np.array_equal(img_pal[src_index], master_pal[target_index]):
index_map[src_index] = target_index
img_data = np.asarray(img).reshape(-1, 2)
acep_data = []
for pair in img_data:
acep_data.append(index_map[pair[0]] * 16 + index_map[pair[1]])
img.close()
os.remove(work_filename)
outputpath_base = pathlib.PurePath(filepath).stem
if ascii_option:
a = 0
out_str = ""
for b in acep_data:
out_str += '0x' + format(b, '02X') + ','
a += 1
if a == 16:
out_str += '\n'
a = 0
else:
out_str += ' '
with open(outputpath_base + '.txt', 'w') as f:
f.write(out_str)
else:
outputpath = pathlib.PurePath(filepath).stem + '.acp'
with open(outputpath_base + '.acp', 'wb') as f:
f.write(bytes(acep_data))
return
if __name__ == '__main__':
ascii_option = False
size_option = '600x448'
target_paths = []
argvs = sys.argv
for arg in argvs[1:]:
if arg == '-ascii':
ascii_option = True
elif arg == '-keep':
size_option = 'keep'
elif re.compile('^-\d+x\d+$').search(arg):
size_option = arg[1:]
else:
target_paths.append(arg)
if len(target_paths) == 0:
print('Usage: %s [-ascii] [-keep] [-WxH] filename ...' % argvs[0])
quit()
for filepath in target_paths:
convert2acp(filepath, size_option, ascii_option)
print('Done!');