LoginSignup
14
19

More than 5 years have passed since last update.

Pillow(PIL)で1文字の画像をたくさん生成する

Last updated at Posted at 2015-10-18

モチベーション

とある事情から複数の言語でOCRしてみようと思ったのですが、どうも自由に使えそうなデータセットがなく、自作する必要が出てきたのでスクリプトを組みました。

使ったもの

Pythonの画像処理ライブラリである Pillow(PIL) を使います。
http://pillow.readthedocs.org/en/3.0.x/index.html

生成する画像

1文字につき1画像を生成します。

スクリプト

生成部分本体のコードは以下の通りです。

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

def generate_char_img(char, fontname='Osaka', size=(64, 64)):
    img=Image.new('L', size, 'white')
    draw = ImageDraw.Draw(img)
    fontsize = int(size[0]*0.8)
    font = ImageFont.truetype(fontname, fontsize)

    # adjust charactor position.
    char_displaysize = font.getsize(char)
    offset = tuple((si-sc)//2 for si, sc in zip(size, char_displaysize))
    assert all(o>=0 for o in offset)

    # adjust offset, half value is right size for height axis.
    draw.text((offset[0], offset[1]//2), char, font=font, fill='#000')
    return img

def save_img(img, filepath):
    img.save(filepath, 'png')

gistには実行コード全体を置いときました。
https://gist.github.com/lazykyama/dabe526246d60fa937d1
(2015/10/18 23:47追記:Image.save()の仕様か、アルファベット大文字小文字のファイル名が区別されないようなので少し修正しています。ご注意ください。)

なお、各言語ごとに文字リストを生成するには以下のようにすればOKです。

英語(アルファベット大文字小文字+数字)

eng_char_list = list(string.digits+string.ascii_letters)

( stringモジュールのリファレンス → http://docs.python.jp/3.3/library/string.html )

日本語

頑張って適当にWikipediaとかから文字を抜いてきましょう。

他の言語

(゚⊿゚)シラネ

注意

  • フォント名は、実際の *.ttf ファイルの名前を指定する必要があるので注意
    • 情弱ゆえ見事にハマった次第

参考

14
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
19