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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24085996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:56:45  来源:igfitidea点击:

How I can load a font file with PIL.ImageFont.truetype without specifying the absolute path?

pythonubuntufontspython-imaging-library

提问by NeoWang

When I write the code in Windows, this code can load the font file just fine:

当我在 Windows 中编写代码时,这段代码可以很好地加载字体文件:

ImageFont.truetype(filename='msyhbd.ttf', size=30);

I guess the font location is registered in Windows registry. But when I move the code to Ubuntu, and copy the font file over to /usr/share/fonts/, the code cannot locate the font:

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

 self.font = core.getfont(font, size, index, encoding)
 IOError: cannot open resource

How can I get PIL to find the ttf file without specifying the absolute path?

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

采纳答案by Hugo

According to the PIL documentation, only Windows font directory is searched:

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

On Windows, if the given file name does not exist, the loader also looks in Windows fonts directory.

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

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

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

So you need to write your own code to search for the full path on Linux.

所以需要自己写代码在Linux上搜索全路径。

However, Pillow, the PIL fork, currently has a PR to search a Linux directory. It's not exactly clear yet which directories to search for all Linux variants, but you can see the code here and perhaps contribute to the PR:

但是,PIL fork 的 Pillow 目前有一个 PR 来搜索 Linux 目录。目前尚不清楚要搜索所有 Linux 变体的目录,但您可以在此处查看代码,并可能对 PR 有所贡献:

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

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

回答by Giovanni G. PY

To me worked this on xubuntu:

对我来说,这是在 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()

enter image description here

在此处输入图片说明

Windows version

视窗版本

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()

The output is the same as aboveenter image description here

输出和上面一样在此处输入图片说明

回答by Rick

On mac, I simply copy the font file Arial.ttf to the project directory and everything works.

在 mac 上,我只需将字体文件 Arial.ttf 复制到项目目录,一切正常。

回答by Ale

There is a Python fontconfigpackage, whereby one can access system font configuration, The code posted by Jeeg_robot can be changed like so:

有一个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()