
Python Pillowで文字入りの画像を作成します。
Pythonのスクリプトを作成する!
Python Pillowで作成したスクリプトは以下のとおりです。作された画像は、本稿のアイキャッチの画像です。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from PIL import Image, ImageFont, ImageDraw# Image Sizeiw=1024ih=576# Background Colorbgr=225bgg=130bgb=50# Text Colortxr=225txg=225txb=225# Text Attributestext="Pillow"size=96fontfile="/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf"# Image Pathsavefile="/home/usradmin/doc/images/eyecatch_pillow_01.png"img=Image.new("RGB",(iw,ih),(bgr,bgg,bgb))draw = ImageDraw.Draw(img)font = ImageFont.truetype(fontfile,size)draw.text((iw/3+10,ih/2-40), text ,(txr,txg,txb), font=font, anchor="mm")img.save(savefile) | 
draw.text関数の引数で指定するテキストの位置は、つどつど調整が必要です。このあたりも、自動化できればよいのですが・・・
おわりに
Python Pillowを使うときれいな画像を簡単に自動生成することができます。画像ソフトを使用して作成するより、かなり作業が効率化できます。
