导入自定义TrueType字体时出现PIL OSError

时间:2020-04-24 23:19:57

标签: python python-3.x python-imaging-library

我已经安装了要在PIL中使用的字体(burbank.ttf)。我仔细检查并安装了它,但是运行以下代码时出现错误:

from PIL import Image, ImageDraw, ImageFont

img = Image.open('blended.png')
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('burbank.ttf, 15')
draw.text((256, 76.8), "test", font=font)

错误:

Traceback (most recent call last):
  File "/home/pi/Desktop/Item Shop/txttest.py", line 5, in <module>
    font = ImageFont.truetype('burbank.ttf, 15')
  File "/home/pi/.local/lib/python3.8/site-packages/PIL/ImageFont.py", line 640, in truetype
    return freetype(font)
  File "/home/pi/.local/lib/python3.8/site-packages/PIL/ImageFont.py", line 637, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/pi/.local/lib/python3.8/site-packages/PIL/ImageFont.py", line 185, in __init__
    self.font = core.getfont(
OSError: cannot open resource

我尝试指定字体的直接路径,但这不起作用。我对字体了解不多,但是为了将其与PIL结合使用,我将.otf文件转换为.ttf。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

可能会更改:

font = ImageFont.truetype('burbank.ttf, 15')

font = ImageFont.truetype('burbank.ttf', 15)

如果无法解决您的问题。 可能的原因之一:找不到burank.ttf的路径,请尝试提供字体的所有路径。