Skip to content

Instantly share code, notes, and snippets.

@pc223
Forked from feltmax/convert.py
Last active July 25, 2021 18:50
Show Gist options
  • Save pc223/131e1c12b39b279ce3e0096ecc386c78 to your computer and use it in GitHub Desktop.
Save pc223/131e1c12b39b279ce3e0096ecc386c78 to your computer and use it in GitHub Desktop.
ttf2png (convert ttf font files to png images)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Tamflex
# Released under the MIT license
# https://opensource.org/licenses/mit-license.php
# reference:
# http://stackoverflow.com/questions/4458696/finding-out-what-characters-a-font-supports
import os
import re
import sys
import PIL
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from itertools import chain
from fontTools.ttLib import TTFont
from fontTools.unicode import Unicode
def load_font(path):
ttf = TTFont(
path, 0, verbose=0, allowVID=0,
ignoreDecompileErrors=True, fontNumber=-1)
chars = chain.from_iterable(
[y + (Unicode[y[0]], )
for y in x.cmap.items()] for x in ttf['cmap'].tables)
chars = set(chars)
chars = sorted(chars, key=lambda c: int(c[0]))
# Use this for just checking if the font contains the codepoint given as
# second argument:
# char = int(sys.argv[2], 0)
# print(Unicode[char])
# print(char in (x[0] for x in chars))
ttf.close()
return chars
def convert_ttf(path, fontsize=60, w=96, h=96):
base = os.path.basename(path)
dirs = os.path.splitext(base)[0]
if not os.path.exists(dirs):
os.mkdir(dirs)
chars = load_font(path)
font = ImageFont.truetype(path, fontsize)
for c in chars:
if re.match('.notdef|nonmarkingreturn|.null', c[1]):
continue
img = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(img)
(ws, hs) = font.getsize(chr(c[0]))
wb = (w - ws) * 0.5
hb = (h - hs) * 0.5
draw.text((wb, hb), chr(c[0]), (0, 0, 0), font=font)
draw = ImageDraw.Draw(img)
img.save('{}/{:d}.png'.format(dirs, c[0]))
if __name__ == '__main__':
# chars = load_font(sys.argv[1])
# for c in chars:
# print(c[0],c[1],c[2])
convert_ttf(sys.argv[1], 80)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Tamflex
# Released under the MIT license
# https://opensource.org/licenses/mit-license.php
# reference:
# http://stackoverflow.com/questions/4458696/finding-out-what-characters-a-font-supports
import os
import re
import sys
import PIL
import numpy as np
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def erase_null(path):
fs = os.listdir(path)
nd = np.array(Image.open('{}/0.png'.format(path)))
for f in fs:
if f == '0.png':
continue
data = np.array(Image.open('{}/{}'.format(path, f)))
if np.array_equal(nd, data):
print '{}/{}'.format(path, f)
os.remove('{}/{}'.format(path, f))
def erase_white(path):
fs = os.listdir(path)
for f in fs:
data = np.array(Image.open('{}/{}'.format(path, f)))
if data.sum() == data.size * 255:
print '{}/{}'.format(path, f)
os.remove('{}/{}'.format(path, f))
if __name__ == '__main__':
erase_white(sys.argv[1])
erase_null(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment