rasterizer using PIL

This commit is contained in:
Rusty Striker 2024-01-12 14:39:30 +02:00
parent 1011c7b5e9
commit 565a717118
Signed by: RustyStriker
GPG key ID: 9DBDBC7C48FC3C31

15
rasterizer.py Normal file
View file

@ -0,0 +1,15 @@
from PIL import Image, ImageDraw, ImageFont
import numpy as np
def text_to_matrix(text, size, font):
pil_font = ImageFont.truetype(font, size=size // len(text), encoding="unic")
canvas = Image.new('RGB', [size + 20, size + 20], (255, 255, 255))
draw = ImageDraw.Draw(canvas)
black = "#000000"
draw.text((10, 10), text, font=pil_font, fill=black)
m = (255 - np.asarray(canvas)) / 255.0
xs, ys, _ = np.where(m > 0.1) # Never trust float comparison
m_trim = m[min(xs):max(xs) + 1, min(ys): max(ys) + 1]
return m_trim