Created
April 5, 2012 17:30
-
-
Save Boldewyn/2312704 to your computer and use it in GitHub Desktop.
Convert font to a series of PNG images
This file contains 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
#!/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