我必须在图像上写一些文本。我正在使用PIL,但无法在此图片上设置特定的字体。我尝试使用下面的代码来执行此操作,但是似乎字体和字体大小不受我的代码的影响。
PS:字体文件与python脚本位于同一目录
#text => text to write on .jpeg
def writeImage(text):
img = Image.new('RGB', (1920, 1080), color = (255,255,255))
draw = ImageDraw.Draw(img)
#load font
fnt = ImageFont.truetype("constan.ttf", 36)
#write text and save image
draw.text((100,100), text, fill=(0,0,0))
img.save("recap.jpg")
答案 0 :(得分:1)
在绘制文本时,您需要专门告诉它使用哪种字体:
# note the font=fnt at the end
draw.text((100, 100), text, fill=(0, 0, 0), font=fnt)
这样,您可以轻松加载数十种或数百种字体,并为每个文本选择应使用哪种字体呈现。