Spaces:
Sleeping
Sleeping
File size: 1,286 Bytes
ca8a804 5493b18 ca8a804 e0cd5b1 ca8a804 e0cd5b1 5493b18 e0cd5b1 5493b18 2f49e43 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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")
|