我正在嘗試動態增加圖像的大小,以提供給draw.text()
的字體和文本。PIL如何縮放與圖像上繪製的文字相關的圖像
Orignal問題是基於名稱和字體用戶選擇創建簽名圖像。
這裏是我的代碼
from PIL import (Image, ImageDraw, ImageFont,)
width=20
height=20
selected_font='simply_glomrous.ttf'
font_size=30
img = Image.new('RGBA', (width, height), (255, 255, 255, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(selected_font, font_size)
draw.text((0,0), "Adil Malik", (0,0,0), font)
img.save('signature.png')
但我仍然有在寬度和高度定義相同的圖像尺寸。我們可以根據字體和大小動態調整圖像大小嗎?
注意:這個問題是相反this stackoverflow question
但是,我的問題仍然存在。我不知道文本的長度是多少。所以我不能從固定圖像大小開始。 –