Pillowでキャンバスにテキストを描画する

スポンサーリンク

ライブラリを読み込む

Pillowでキャンバスにテキストを描画するためのライブラリを読み込みます。

from IPython.display import display
from PIL import Image, ImageFont, ImageDraw

テキストを描画する関数を作成する

テキストを描画する関数を作成します。

def drawtxts(x, y, txts, rgb, size):
  font=ImageFont.truetype("meiryob.ttc", size) #フォントとサイズを指定
  for txt in txts:
    draw.text((x, y), txt, rgb, font)
    y=y+30

テキストを描画する

キャンバスにテキストを描画して表示します。

base=Image.new("RGB", (1000, 300), (66, 66, 66)) #キャンバスのサイズとRGBを指定
draw=ImageDraw.Draw(base)

x, y, txts, rgb, size=0, 0, ["test1", "test2"], (255, 255, 255), 30
drawtxts(x, y, txts, rgb ,size) #表示位置、テキスト、文字色、文字サイズを指定

display(base) #画像を表示

コメント

タイトルとURLをコピーしました