无法在 Python 图片库 (pillow) 上加载字体

Can't load font on Python Image Library (pillow)

我必须在图片上写一些文字。我用的是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")

绘制文字时,需要具体告诉它使用哪种字体:

# note the font=fnt at the end
draw.text((100, 100), text, fill=(0, 0, 0), font=fnt)

通过这种方式,您可以轻松加载数十或数百种字体,并为每段文本选择应使用哪种字体呈现。