we’ll build a simple system using Python that converts your handwritten characters into a TrueType font (.ttf).
Step 1 – Install the Required Library
First, we need a Python library called Pillow. It helps us create and edit images.
Open your terminal or command prompt and run:
pip install pillow
Once it installs, you're ready to move to the next step.
Step 2 – Create a Template for Handwriting
Instead of manually drawing boxes for every letter, we can let Python generate a template automatically.
Create a new file called:
create_template.py
Add the following code:
from PIL import Image, ImageDraw, ImageFont
font_size = 80
image_width = 2600
image_height = 1200
font_path = "arial.ttf"
output_image_path = "template_image.png"
characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
template_image = Image.new("RGB", (image_width, image_height), "white")
draw = ImageDraw.Draw(template_image)
font = ImageFont.truetype(font_path, font_size)
x = 50
y = 100
for char in characters:
draw.text((x, y-60), char, fill="black", font=font)
draw.rectangle([x, y, x+80, y+80], outline="black")
x += 120
if x > 2400:
x = 50
y += 150
template_image.save(output_image_path)
print("Template created:", output_image_path)
Now run the script:
python create_template.py
After running it, a new image will appear in your folder:
template_image.png
This image contains boxes for every letter.
Step 3 – Fill the Template With Your Handwriting
Now comes the fun part.
Open the image:
template_image.png
You can use any drawing tool like:
Paint
Photoshop
Inkscape
Draw your handwriting inside each box.
For example:
[A] [B] [C]
[D] [E] [F]
Once you're done, save the file as:
filled_template_image.png
Step 4 – Extract Each Character
Now we need to cut each letter into separate images.
Create a new Python file called:
extract_characters.py
Add this code:
from PIL import Image
import os
image = Image.open("filled_template_image.png")
characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
os.makedirs("character_images", exist_ok=True)
x = 50
y = 100
for char in characters:
crop = image.crop((x, y, x+80, y+80))
crop.save(f"character_images/{char}.png")
x += 120
if x > 2400:
x = 50
y += 150
print("Characters extracted")
Run it:
python extract_characters.py
A new folder will appear:
character_images
Inside it, you'll see files like:
A.png
B.png
C.png
Each letter is now stored as its own image.
Step 5 – Convert the Images Into a Font
Now we turn those images into a real font.
Create another file called:
create_handwriting_font.py
Add the script that converts the character images into a .ttf font.
This script uses FontForge to map each character image to its Unicode value and generate the font file.
import os
import fontforge
from PIL import Image, ImageDraw, ImageFont
font_size = 80
image_width = 2600
image_height = 1200
font_path = "arial.ttf"
output_image_path = "template_image.png"
filled_template_image_path = "filled_template_image.png"
output_directory = "character_images"
output_font_path = "handwritten_font.ttf"
english_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
def create_template_image():
template_image = Image.new("RGB", (image_width, image_height), "white")
draw = ImageDraw.Draw(template_image)
font = ImageFont.truetype(font_path, font_size)
x = 50
y = 50
character_positions = {}
for char in english_characters:
draw.text((x, y), char, fill="black", font=font)
box = (x, y+80, x+font_size, y+80+font_size)
draw.rectangle(box, outline="black")
character_positions[char] = box
x += font_size * 2
if x > image_width - font_size:
x = 50
y += font_size * 2 template_image.save(output_image_path)
print("Template created")
return character_positions
def crop_character_images(character_positions):
img = Image.open(filled_template_image_path)
os.makedirs(output_directory, exist_ok=True)
for char, pos in character_positions.items():
crop = img.crop(pos) crop.save(os.path.join(output_directory, f"{char}.png"))
print("Characters cropped")
def create_font(character_positions):
font = fontforge.font()
font.familyname = "HandwrittenEnglish"
font.fontname = "HandwrittenEnglish"
for char in english_characters:
unicode_val = ord(char)
glyph = font.createChar(unicode_val)
image_path = os.path.join(output_directory, f"{char}.png")
if os.path.exists(image_path): glyph.importOutlines(image_path)
glyph.addExtrema()
glyph.simplify() glyph.correctDirection()
font.generate(output_font_path)
print("Font generated:", output_font_path)
def main():
positions = create_template_image()
print("Fill template and save as filled_template_image.png")
input("Press Enter after filling handwriting...") crop_character_images(positions)
create_font(positions)
if name == "main":
main()
After running the script, Python will generate:
handwritten_font.ttf
Step 6 – Install Your Font
Once the font file is created, install it on your system.
Then open any program like:
Microsoft Word
Notion
Google Docs
Final thoughts
However,in my case the font installs successfully, but it does not appear in Microsoft Word yet. This means there is still something that needs to be fixed in the font generation process.
So in my next blog, I will explain:
Why the font is not appearing in Word
What changes are needed in the code
How to properly generate a working font
Stay tuned for the next part where we will fix this issue and make the handwriting font work properly.
Top comments (0)