我使用以下两种方法为.ttf字体文件生成文本预览图像
PIL方法:def make_preview(text, fontfile, imagefile, fontsize=30):
try:
font = ImageFont.truetype(fontfile, fontsize)
text_width, text_height = font.getsize(text)
img = Image.new('RGBA', (text_width, text_height))
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, font=font, fill=(0, 0, 0))
return True
except:
return False
ImageMagick方法:
^{pr2}$
这两种方法在大多数情况下都可以创建正确的预览图像,但在某些情况下(<;2%)会字体.getsize(文本)只是无法获得正确的文本大小,从而导致文本溢出提供的画布。ImageMagick也有同样的问题。在
示例字体和预览:
是否有可能检测到这样的文本溢出并按照我们的预期绘制文本以适合画布?在