mirror of
https://github.com/radex/radmatrix.git
synced 2024-10-30 00:44:51 +00:00
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
|
# read gfx folder
|
||
|
|
||
|
import os
|
||
|
import sys
|
||
|
import PIL
|
||
|
from PIL import Image
|
||
|
|
||
|
def get_all_gfx():
|
||
|
gfx = []
|
||
|
for root, dirs, files in os.walk("gfx"):
|
||
|
for file in files:
|
||
|
# file name without extention
|
||
|
name = os.path.splitext(file)[0]
|
||
|
gfx.append((name, os.path.join(root, file)))
|
||
|
|
||
|
gfx.sort()
|
||
|
return gfx
|
||
|
|
||
|
def to_code(name, path):
|
||
|
img = Image.open(path)
|
||
|
img = img.convert("1", dither=Image.Dither.FLOYDSTEINBERG)
|
||
|
w, h = img.size
|
||
|
colors = img.getcolors()
|
||
|
print(colors)
|
||
|
|
||
|
code = f"uint32_t gfx_{name}[{h}] = {'{'}\n"
|
||
|
|
||
|
for y in range(h):
|
||
|
code += " 0b"
|
||
|
for x in range(w):
|
||
|
px = img.getpixel((x, y))
|
||
|
code += '1' if px == 0 else '0'
|
||
|
code += ",\n"
|
||
|
|
||
|
code += "};\n\n"
|
||
|
return code
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
gfx = get_all_gfx()
|
||
|
gfx_code = ''.join(to_code(name, path) for name, path in gfx)
|
||
|
|
||
|
header = f"#pragma once\n#include <Arduino.h>\n\n/* DO NOT MODIFY - AUTOGENERATED! */\n\n"
|
||
|
|
||
|
index = f"uint32_t* frames[{len(gfx)}] = {'{'}\n"
|
||
|
for name, _ in gfx:
|
||
|
index += f" gfx_{name},\n"
|
||
|
index += "};\n"
|
||
|
|
||
|
with open("src/gfx.h", "w") as f:
|
||
|
f.write(header)
|
||
|
f.write("#define GFX_COUNT " + str(len(gfx)) + "\n\n")
|
||
|
f.write(gfx_code)
|
||
|
f.write(index)
|
||
|
|
||
|
print("gfx.h generated!")
|