Skip to content

Instantly share code, notes, and snippets.

@Boldewyn
Created April 5, 2012 17:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Boldewyn/2312704 to your computer and use it in GitHub Desktop.
Save Boldewyn/2312704 to your computer and use it in GitHub Desktop.
Convert font to a series of PNG images
#!/usr/bin/python
"""Convert all defined glyphs from a font to PNG images
Needs FontForge, ImageMagick and pngcrush installed."""
import fontforge as ff
import os
import subprocess
import sys
def main(args):
font = args[0]
if not os.path.isfile(font):
raise ValueError("Is no file: %s" % font)
folder = os.path.splitext(os.path.basename(font))[0]
if not os.path.isdir(folder):
os.mkdir(folder)
selection = ff.open(font).selection.all().byGlyphs
for glyph in selection:
cp = glyph.unicode
xcp = '%04X' % cp
fn = "%s/U+%s.png" % (folder, xcp)
tfn = "%s/U+%s.tmp.png" % (folder, xcp)
subprocess.call([
"convert", "-background", "none", "-gravity", "center",
"-size", "16x16", "-fill", "black", "-font", font,
"-pointsize", "16", "label:"+unichr(cp).encode("UTF-8"), tfn])
subprocess.call(["pngcrush", "-q", "-rem", "alla", tfn, fn])
subprocess.call(["rm", tfn])
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment