Created
July 27, 2022 01:38
-
-
Save Dobby233Liu/25e1fa2f64dd70f8f0f68079cef00753 to your computer and use it in GitHub Desktop.
attempted sprite font generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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