らんだむな記憶

blogというものを体験してみようか!的なー

FreeTypeで文字を書く(2)

FreeTypeで文字を書く - らんだむな記憶の続きがほんのりやりたくなった。
freetype-py/agg-trick.py at master · rougier/freetype-py · GitHubがいいサンプルなので以下のようにしてみた。いい感じ。

def save_image(face, gid):
    face.load_glyph(gid)
    bitmap = face.glyph.bitmap
    width, height = bitmap.width, bitmap.rows
    left, top = face.glyph.bitmap_left, face.glyph.bitmap_top
    shift = 120
    W, H = 1000, 1000
    Z = np.zeros( (H, W), dtype=np.ubyte )
    buff = np.array(bitmap.buffer, dtype=np.ubyte).reshape((height, width))
    Z[H-(top+shift):H-(top+shift)+height,left:left+width].flat |= buff[:,:width].flatten()
    Z = 0xff - Z
    im = Image.fromarray(Z, mode='L')
    im.save("{}.png".format(gid), "PNG")