esp32-sk6812/font/convert.py

72 lines
1.6 KiB
Python
Raw Permalink Normal View History

2019-12-23 19:55:40 +01:00
#!/usr/bin/env python3
from PIL import Image
import sys
if len(sys.argv) < 3:
2019-12-23 21:53:36 +01:00
print(f"usage: {sys.argv[0]} <bitmap> <characters> <output>")
2019-12-23 19:55:40 +01:00
exit(1)
inputfile = sys.argv[1]
charfile = sys.argv[2]
outputfile = sys.argv[3]
im = Image.open(inputfile)
cols, rows = im.size
offset = 4 # hardcoded 'space'
startpos = offset
fixedwidth = 9
charpos = [0] # hardcoded 'space'
chardata = [0x00] * offset # hardcoded 'space'
charwidth = [offset] # hardcoded 'space'
for col in range(cols):
coldata = 0
for row in range(rows):
px = 0 if im.getpixel( (col, row) ) else 1
coldata |= (px << row)
print("{:016b}".format(coldata))
width = offset - startpos
chardata.append(coldata)
if width == fixedwidth:
charpos.append(startpos)
charwidth.append(width)
startpos = offset
2019-12-23 21:53:36 +01:00
offset += 1
2019-12-23 19:55:40 +01:00
dataarray = ", ".join([hex(n) for n in chardata])
posarray = ", ".join([str(n) for n in charpos])
widtharray = ", ".join([str(n) for n in charwidth])
asciimap = [0 for i in range(256)]
with open(charfile, "r") as cfile:
s = " " + cfile.read().strip()
for i in range(len(s)):
asciimap[ord(s[i])] = i
asciimaparray = ", ".join([str(n) for n in asciimap])
with open(outputfile, "w") as ofile:
ofile.write("""
#include "font16_data.h"
const uint16_t font_data[{}] = {{ {} }};
const uint16_t font_pos[{}] = {{ {} }};
const uint8_t font_width[{}] = {{ {} }};
const uint8_t font_lut[256] = {{ {} }};
const uint8_t font_height = {};
""".format(len(chardata), dataarray, len(charpos), posarray, len(charwidth), widtharray, asciimaparray, rows))