Python Pillowで文字入りの画像を作成します。
Pythonのスクリプトを作成する!
Python Pillowで作成したスクリプトは以下のとおりです。作された画像は、本稿のアイキャッチの画像です。
from PIL import Image, ImageFont, ImageDraw
# Image Size
iw=1024
ih=576
# Background Color
bgr=225
bgg=130
bgb=50
# Text Color
txr=225
txg=225
txb=225
# Text Attributes
text="Pillow"
size=96
fontfile="/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf"
# Image Path
savefile="/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を使うときれいな画像を簡単に自動生成することができます。画像ソフトを使用して作成するより、かなり作業が効率化できます。