简体   繁体   English

无法在 Python 图像库(枕头)上加载字体

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

I have to write some text over an image.我必须在图像上写一些文字。 I'm using PIL, but I can't set the specific font on this picture.我正在使用 PIL,但我无法在此图片上设置特定字体。 I tried to do it using the code below, but it seems that the font and the font size aren't affected by my code.我尝试使用下面的代码来做到这一点,但似乎字体和字体大小不受我的代码的影响。

PS: the font file is in the same directory of the python script 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")

When drawing text, you need to tell it specifically which font to use:绘制文本时,需要具体告诉它使用哪种字体:

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

This way, you can easily load up tens or hundreds of fonts and choose for each piece of text which font it should be rendered with.通过这种方式,您可以轻松加载数十或数百种字体,并为每段文本选择应使用哪种字体进行渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM