Created
February 28, 2018 16:02
-
-
Save SuoXC/e41854556e15e84351eb59b53ea12aee to your computer and use it in GitHub Desktop.
plot glyphs in a specified font to 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
from PIL import Image, ImageDraw, ImageFont | |
class FontPlotter: | |
def __init__(self,font_size=200,convas_size=256): | |
self._font_size = font_size | |
self._convas_size = convas_size | |
self._offset = int((convas_size - font_size) / 2) | |
self._font = None | |
def loadFont(self,filename): | |
self._font = ImageFont.truetype(filename, size=self._font_size,encoding='unic') | |
def plot(self,char): | |
if self._font is None: | |
raise RuntimeError('no font loaded') | |
img = Image.new("RGB", (self._convas_size, self._convas_size), (255, 255, 255)) | |
draw = ImageDraw.Draw(img) | |
draw.text((self._offset, self._offset), char, (0, 0, 0), font=self._font) | |
return img | |
if __name__ == "__main__": | |
plt = FontPlotter() | |
plt.loadFont('miao.ttf') | |
plt.plot('喵').save('喵.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment