precompute
This commit is contained in:
parent
38bef90d39
commit
2e39a174aa
2 changed files with 69 additions and 0 deletions
BIN
fonts/courbd.ttf
Normal file
BIN
fonts/courbd.ttf
Normal file
Binary file not shown.
69
precomputeFonts.py
Normal file
69
precomputeFonts.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
import os
|
||||||
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
|
|
||||||
|
# Path to the TTF font file
|
||||||
|
fontFileName = "courbd.ttf"
|
||||||
|
font_path = "fonts/" + fontFileName
|
||||||
|
|
||||||
|
# Create the folder to store the character images
|
||||||
|
#if it exists, delete everything in it
|
||||||
|
|
||||||
|
output_folder = "fonts/" + os.path.splitext(fontFileName)[0] + "_images"
|
||||||
|
|
||||||
|
if os.path.exists(output_folder):
|
||||||
|
for file in os.listdir(output_folder):
|
||||||
|
os.remove(output_folder + "/" + file)
|
||||||
|
else:
|
||||||
|
os.makedirs(output_folder)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Define the size of the square image
|
||||||
|
image_size = 32
|
||||||
|
font_size = image_size
|
||||||
|
|
||||||
|
# Load the font
|
||||||
|
|
||||||
|
font = ImageFont.truetype(font_path, font_size)
|
||||||
|
|
||||||
|
# Loop through each character in the font
|
||||||
|
for char in range(32, 127): # ASCII range from space to tilde (~)
|
||||||
|
# Create a new image with a white background
|
||||||
|
image = Image.new("L", (image_size, image_size), color=255)
|
||||||
|
draw = ImageDraw.Draw(image)
|
||||||
|
|
||||||
|
# Calculate the position to center the character
|
||||||
|
l, t, r, b = font.getbbox(chr(char))
|
||||||
|
|
||||||
|
char_width, char_height = r - l, b - t
|
||||||
|
x = (image_size - char_width) // 2 - l
|
||||||
|
y = (image_size - char_height) // 2 - t
|
||||||
|
|
||||||
|
# Draw the character in black
|
||||||
|
draw.text((x, y), chr(char), font=font, fill=0)
|
||||||
|
|
||||||
|
# Save the image
|
||||||
|
image.save(f"{output_folder}/{char}.png")
|
||||||
|
|
||||||
|
# Create a new image to store all characters
|
||||||
|
all_image = Image.new("L", (image_size * 12, image_size * 8), color=255)
|
||||||
|
all_draw = ImageDraw.Draw(all_image)
|
||||||
|
|
||||||
|
# Loop through each character in the font
|
||||||
|
for char in range(32, 127): # ASCII range from space to tilde (~)
|
||||||
|
# Calculate the row and column of the character in the "all" image
|
||||||
|
row = (char - 32) // 12
|
||||||
|
col = (char - 32) % 12
|
||||||
|
|
||||||
|
# Calculate the position to draw the character in the "all" image
|
||||||
|
x = col * image_size
|
||||||
|
y = row * image_size
|
||||||
|
|
||||||
|
# Draw the character in black
|
||||||
|
all_draw.text((x, y), chr(char), font=font, fill=0)
|
||||||
|
|
||||||
|
# Save the "all" image
|
||||||
|
all_image.save(f"{output_folder}/all.png")
|
||||||
|
|
||||||
|
print("Character images exported successfully!")
|
Loading…
Add table
Add a link
Reference in a new issue