如何在不指定绝对路径的情况下使用PIL.ImageFont.truetype加载字体文件?

Neo*_*ang 18 python ubuntu fonts python-imaging-library

当我在Windows中编写代码时,此代码可以正常加载字体文件:

ImageFont.truetype(filename='msyhbd.ttf', size=30);
Run Code Online (Sandbox Code Playgroud)

我想字体位置是在Windows注册表中注册的.但是,当我将代码移动到Ubuntu,并将字体文件复制到/ usr/share/fonts /时,代码无法找到该字体:

 self.font = core.getfont(font, size, index, encoding)
 IOError: cannot open resource
Run Code Online (Sandbox Code Playgroud)

如何在不指定绝对路径的情况下让PIL找到ttf文件?

Gio*_* PY 24

对我来说,在xubuntu上工作:

from PIL import Image,ImageDraw,ImageFont

# sample text and font
unicode_text = u"Hello World!"
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 28, encoding="unic")

# get the line size
text_width, text_height = font.getsize(unicode_text)

# create a blank canvas with extra space between lines
canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange")

# draw the text onto the text canvas, and use black as the text color
draw = ImageDraw.Draw(canvas)
draw.text((5,5), u'Hello World!', 'blue', font)

# save the blank canvas to a file
canvas.save("unicode-text.png", "PNG")
canvas.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Windows版本

from PIL import Image, ImageDraw, ImageFont

unicode_text = u"Hello World!"
font = ImageFont.truetype("arial.ttf", 28, encoding="unic")
text_width, text_height = font.getsize(unicode_text)
canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange")
draw = ImageDraw.Draw(canvas)
draw.text((5, 5), u'Hello World!', 'blue', font)
canvas.save("unicode-text.png", "PNG")
canvas.show()
Run Code Online (Sandbox Code Playgroud)

输出与上面相同

  • 这段代码在Ubuntu中对我有用,谢谢! (2认同)

Hug*_*ugo 10

根据PIL文档,只搜索Windows字体目录:

在Windows上,如果给定的文件名不存在,则加载程序也会在Windows字体目录中查找.

http://effbot.org/imagingbook/imagefont.htm

所以你需要编写自己的代码来搜索Linux上的完整路径.

但是,PIL fork的Pillow目前有一个PR来搜索Linux目录.目前尚不清楚哪些目录可以搜索所有Linux变种,但您可以在这里查看代码并可能对PR有所贡献:

https://github.com/python-pillow/Pillow/pull/682

  • 看起来这个问题已在 https://github.com/python-pillow/Pillow/pull/1054 中修复,因此 Linux 和 MacOS 搜索字体时无需使用绝对路径。 (3认同)

Ale*_*Ale 5

有一个Python fontconfig包,可以通过它访问系统字体配置, Jeeg_robot 发布的代码可以像这样更改:

from PIL import Image,ImageDraw,ImageFont
import fontconfig

# find a font file
fonts = fontconfig.query(lang='en')
for i in range(1, len(fonts)):
    if fonts[i].fontformat == 'TrueType':
        absolute_path = fonts[i].file
        break

# the rest is like the original code:
# sample text and font
unicode_text = u"Hello World!"
font = ImageFont.truetype(absolute_path, 28, encoding="unic")

# get the line size
text_width, text_height = font.getsize(unicode_text)

# create a blank canvas with extra space between lines
canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange")

# draw the text onto the text canvas, and use black as the text color
draw = ImageDraw.Draw(canvas)
draw.text((5,5), u'Hello World!', 'blue', font)

# save the blank canvas to a file
canvas.save("unicode-text.png", "PNG")
canvas.show()
Run Code Online (Sandbox Code Playgroud)

  • 这个包不适用于更现代版本的 Python 3。我用 3.10 测试了你的示例,遗憾的是它不再工作了。这绝对是一个不错的答案。但我想留下这个信息,以防有人陷入困境。 (3认同)