Spaces:
Sleeping

bitmap-simulator / generate_text.py
jk
Emdedded in Gradio
2f49e43
Raw History Blame Contribute Delete
1.29 kB
from bdfparser import Font
from PIL import Image, ImageFont, ImageDraw
font = Font("fonts/test.bdf")
print(
f"This font's global size is "
f"{font.headers['fbbx']} x {font.headers['fbby']} (pixel), "
f"it contains {len(font)} glyphs."
)
# =================================
# Draw a centered text
def draw_text(image, text, font, color):
# use a bitmap font
font = ImageFont.load_path("fonts/TSI06x12.pil")
# get image size
width, height = image.size
# get the size of the text
text_width = font.getlength(text)
draw = ImageDraw.Draw(image)
_, _, w, text_height = draw.textbbox((0, 0), text, font=font)
# set the text position
text_x = (width - text_width) / 2
text_y = (height - text_height) / 2
# draw text
draw = ImageDraw.Draw(image)
draw.text((text_x, text_y), text, color, font=font)
new_image = Image.new("RGB", (128, 24))
draw = ImageDraw.Draw(new_image)
draw_text(new_image, "Max-Weber-Platz", font, (255, 255, 255))
new_image.save("output/font_output.bmp")
def generate_text_bitmap(text, width=128, height=24):
# Create new image
new_image = Image.new("RGB", (width, height))
# Draw text
draw_text(new_image, text, font, (255, 255, 255))
new_image.save("./tmp/font.bmp")