Turning a photo of handwriting into an OpenType font (Python pipeline)
fontsopentypefonttoolspotraceimage-segmentationpythonflutter
Justin photographed an alphabet drawn with a Sharpie and asked for it as a font he could write
TLDR posts in. The result, "TLDR Marker", is a CFF-flavored OTF built entirely with Python
(fontTools + potracer), no FontForge or ImageMagick. Version 1.0 had 61 glyphs from one photo;
later photos added lowercase and symbols for full printable ASCII (104 glyphs). The pipeline is
committed and rebuildable (tool/font/ in the TLDR repo).
Stage 1: segment the photo (segment.py)
- Adaptive threshold against an estimated background: box-blur the photo with a large radius (60 px) to get the paper's local brightness, then call a pixel ink if it is darker than background minus 45 and below 200. Handles uneven lighting that a global threshold cannot.
- Connected components by run-length union-find.
- Fixed row bands for each line of the alphabet, then group components that overlap in x within a
row (so
i,j,:and"become one glyph). - Emit
groups.jsonplus overlay and contact-sheet renders for human QA. Drop junk by hand: ink specks, a crossed-out box, ambiguous redraws, duplicates.
Stage 2: build the font (build_font.py)
- Per-row linear baseline fit corrects the photo's tilt; per-row scale normalizes size. Units per em 1000, cap height 700.
- Punctuation is placed by per-character rules (sits on the baseline, hangs from the top, or is centered) because its own bounding box says nothing about where it belongs.
- Binary closing (radius 2) seals glare channels that a Sharpie stroke leaves in a photo.
- Counter-aware hole filling: keep only the N largest holes over a minimum area per glyph, so noise holes vanish but real counters survive (B has a single merged counter; the counter in 4 was only 75 px).
- Upscale 3x bilinear, trace with potracer (pure-Python potrace), draw the paths through
fontTools'
T2CharStringPen, and assemble withFontBuilderas CFF. - Map extra code points to existing glyphs where it makes sense (curly quotes, en dash, minus); a font with no lowercase can alias a to z onto the capitals.
Gotchas
- potracer traces 0/False pixels (image convention: black = 0). Feed it an inverted mask or you trace the paper.
- Install with
python3 -m pip install --user --break-system-packages fonttools potraceron a PEP 668 system. - Photo-specific constants (crop, row splits, glyph-to-character map, holes to keep) belong at the top of the scripts; each new photo needs them re-tuned.
- QA by rendering every glyph on baselines with PIL, not just by opening the font.
- Check the font's cmap for coverage instead of trusting notes; coverage changed across versions.
Using it in a Flutter app
Declare it under fonts: in pubspec.yaml. Missing characters fall back per character to the
default font. TLDR applies it only inside the editor (readers see the normal font) by wrapping the
editor in DefaultTextStyle.merge; see flutter_quill rich-text editor gotchas (markdown, IME, spell check, fonts).