#! /usr/bin/env python # coding: utf-8 # coding=utf-8 # -*- coding: utf-8 -*- # vim: fileencoding=utf-8 import sys from PIL import ImageFont from PIL import Image from PIL import ImageDraw fontFile = 'KouzanGyoushoOTF.otf' font = ImageFont.truetype(fontFile, 64, encoding='utf-8') text = u'謹賀新年' w, h = font.getsize(text) print '%s w:%d h:%d' % (text, w, h) im = Image.new('RGB', (w, h), (255,255,255)) draw = ImageDraw.Draw(im) draw.text((0, 0), text, fill=(0,0,0), font=font) # im.show() // 即、プレビューする時はここのコメントアウトを外す im.save('image.png')
※PILを事前にインストールしておいて下さい。
※以下のファイルをPythonのプログラムファイルと同じ階層に置いて下さい。
衡山毛筆フォント行書
http://musashi.or.tv/kouzangyousho.htm
背景を透過させる場合。
#! /usr/bin/env python # coding: utf-8 # coding=utf-8 # -*- coding: utf-8 -*- # vim: fileencoding=utf-8 import sys from PIL import ImageFont from PIL import Image from PIL import ImageDraw fontFile = 'KouzanGyoushoOTF.otf' font = ImageFont.truetype(fontFile, 64, encoding='utf-8') text = u'謹賀新年' w, h = font.getsize(text) print '%s w:%d h:%d' % (text, w, h) im = Image.new('RGBA', (w, h), (255,255,255,0)) draw = ImageDraw.Draw(im) draw.text((0, 0), text, fill=(0,0,0), font=font) #im.show() # 即、プレビューする時はここのコメントアウトを外す im.save('image.png')
【参考】
Python PIL テキストサイズ取得時の注意点 - 個人的メモを色々と
http://takadamemo.wikidot.com/python004