rasterizer using PIL
This commit is contained in:
parent
1011c7b5e9
commit
565a717118
1 changed files with 15 additions and 0 deletions
15
rasterizer.py
Normal file
15
rasterizer.py
Normal 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
|
||||||
|
|
Loading…
Reference in a new issue