Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Dobby233Liu
Created July 27, 2022 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobby233Liu/25e1fa2f64dd70f8f0f68079cef00753 to your computer and use it in GitHub Desktop.
Save Dobby233Liu/25e1fa2f64dd70f8f0f68079cef00753 to your computer and use it in GitHub Desktop.
attempted sprite font generator
from PIL import Image, ImageFont, ImageDraw, ImageColor
SEPERATOR_COLOR = ImageColor.getrgb("#0000ffff")
GLYPHS = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:<[=>\\{]!|@~_\"`$%'()&}+*,-^#?/;"
SIZE = 12
ANCHOR = "la"
FILL = "white"
FEATURES = ""
font = ImageFont.truetype("arial.ttf", SIZE)
with Image.new("RGBA", (1, 1), (0, 0, 0, 0)) as image:
for i in list(GLYPHS):
height = font.getbbox(i, anchor=ANCHOR, features=FEATURES)[1] + font.getbbox(i, anchor=ANCHOR, features=FEATURES)[3]
image = image.resize((
image.size[0] + round(font.getlength(i, features=FEATURES)),
max(height, image.size[1])
))
draw_x = 0
draw = ImageDraw.Draw(image)
for i in list(GLYPHS):
draw.line([draw_x, 0, draw_x, image.size[1]], fill=SEPERATOR_COLOR)
draw.text((draw_x, 0), i, font=font, anchor=ANCHOR, fill=FILL, features=FEATURES)
draw_x += font.getlength(i, features=FEATURES)
draw.line([draw_x, 0, draw_x, image.size[1]], fill=SEPERATOR_COLOR)
image.save("font.png", "png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment