我试图根据传递给draw.text()
的字体和文本动态增加图像大小。
原始问题是基于用户选择的字体和名称创建签名图像。
这是我的代码:
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')
但是我仍然在使用宽度和高度定义的相同图像尺寸。我们可以根据字体及其大小动态调整图像大小吗?注意:这个问题与 这个 stackoverflow 问题 相反。