-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonify.py
60 lines (50 loc) · 1.57 KB
/
jsonify.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
"""
Python script for converting image files (png) into OCaml arrays of hexademical
color values, to be used in conjunction with the OCaml Graphics library to
generate sprites for OCamlMon.
Authors: CJ Lee, Kimmy Lin, Sabrina Li
"""
from PIL import Image
import os
PATH = "json_images"
def rgb_to_hex(r, g, b, a):
if a == 0:
hex = '"transp"'
else:
hex = '"0x{:02x}{:02x}{:02x}"'.format(r, g, b)
return hex
def list_to_array(lst):
arr = ['[', '\n']
for row in lst:
arr.append('[')
for col in row:
arr.append(col)
arr.append(', ') if col != '"transp"' else arr.append(' , ')
arr[-1] = ""
arr.append('],\n')
arr[-1] = "]\n"
arr.append(']')
return "".join(arr)
if __name__ == "__main__":
input = input("Full image file name: ")
try:
print(f"Opening image {input}...")
img = Image.open(input)
pixels = img.convert('RGBA')
size = img.size
print("Converting image to JSON...")
lst = []
for row in range(size[0]):
lst.append([])
for col in range(size[1]):
lst[-1].append(rgb_to_hex(*(pixels.getpixel((row, col)))))
lst = list(map(list, zip(*lst)))
result = list_to_array(lst)
print("Creating output file...")
if not os.path.exists(PATH):
print(f"Creating directory /{PATH}...")
os.mkdir(PATH)
output = open(PATH+f"/{os.path.splitext(input)[0]}", "w")
output.write(result)
except:
print("Invalid image file")