导入自定义truetype字体时出错

2024-06-28 10:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经安装了一个字体(burbank.ttf),我正试图在PIL中使用它。我仔细检查后,它已安装,但运行以下代码时出现错误:

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

我尝试过指定字体的直接路径,但不起作用。我对字体了解不多,但我将.otf文件转换为.ttf,以便与PIL一起使用

感谢您的帮助


Tags: inpyhomepilliblocallinepi
1条回答
网友
1楼 · 发布于 2024-06-28 10:25:17

可能会改变:

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

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

如果它不能解决你的问题。 一个可能的原因是:它找不到burank.ttf的路径,请尝试给出字体的所有路径

相关问题 更多 >