Move the image loader to shared

This commit is contained in:
Pyrex 2024-02-26 16:05:07 -08:00
parent 72599b7d00
commit d05e382063
2 changed files with 26 additions and 22 deletions

View File

@ -25,29 +25,22 @@ def main(font_name, n_glyphs, fname_png, fname_c):
def load_glyphs(fname_png: str):
with Image.open(fname_png) as im:
width = im.width
height = im.height
width, height, data = shared.load_image(fname_png)
monochrome = [pixel_to_monochrome(p) for p in data]
assert width % 8 == 0, "width must be a multiple of 8"
assert height % 8 == 0, "height must be a multiple of 8"
glyphs = []
for gy in range(0, height, 8):
for gx in range(0, width, 8):
glyph = 0
for py in range(0, 8):
for px in range(0, 8):
x = gx + px
y = gy + py
if monochrome[y * width + x]:
glyph |= 1 << (py * 8 + px)
glyphs.append(glyph)
data = list(im.convert("RGBA").getdata())
monochrome = [pixel_to_monochrome(p) for p in data]
glyphs = []
for gy in range(0, height, 8):
for gx in range(0, width, 8):
glyph = 0
for py in range(0, 8):
for px in range(0, 8):
x = gx + px
y = gy + py
if monochrome[y * width + x]:
glyph |= 1 << (py * 8 + px)
glyphs.append(glyph)
return glyphs
return glyphs
def pixel_to_monochrome(rgba: Tuple[int, int, int, int]):

View File

@ -1,6 +1,17 @@
from PIL import Image
from jinja2 import Environment, BaseLoader, select_autoescape
templates = Environment(
loader=BaseLoader(),
autoescape=select_autoescape(),
)
def load_image(fname):
with Image.open(fname) as im:
width = im.width
height = im.height
assert width % 8 == 0, "width must be a multiple of 8"
assert height % 8 == 0, "height must be a multiple of 8"
return width, height, list(im.convert("RGBA").getdata())