Adi*_*lik 16 python python-imaging-library python-3.x
我试图动态增加图像大小相对于给出的字体和文本draw.text().
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')
Run Code Online (Sandbox Code Playgroud)
但我仍然在宽度和高度定义相同的图像大小.我们可以根据字体及其大小动态调整图像大小吗?
注意:此问题与此stackoverflow问题相反
Adi*_*lik 13
不幸的是,没有人能够回答我的问题.
基本上,您无法在设置字体大小时设置固定宽度和高度.两者都相互依赖.因此,如果一个增加,第二个也增加.
所以我想出了另一个解决方案.我只是设置字体大小然后根据该字体大小,我设置宽度和高度.
from PIL import (Image, ImageDraw, ImageFont,)
name = 'Adil Malik'
selected_font='simply_glomrous.ttf'
font_size=30
font = ImageFont.truetype(selected_font, font_size)
font_size = font.getsize(name)
img = Image.new('RGBA', (font_size[0], font_size[0]), (255, 255, 255, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(selected_font, font_size)
draw.text((0,0), name, (0,0,0), font)
img.save('signature.png')
Run Code Online (Sandbox Code Playgroud)