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

时间:2014-06-06 15:53:36

标签: python ubuntu fonts python-imaging-library

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

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

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

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

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

6 个答案:

答案 0 :(得分:19)

让我在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版

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

输出与上面的enter image description here

相同

答案 1 :(得分:9)

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

  

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

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

因此,您需要编写自己的代码来搜索Linux上的完整路径。

然而,PIL分支Pillow目前有一个PR来搜索Linux目录。目前尚不清楚哪些目录可以搜索所有Linux变种,但是你可以在这里查看代码,也许可以为PR做出贡献:

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

答案 2 :(得分:4)

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

答案 3 :(得分:3)

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

答案 4 :(得分:0)

在Mac上,项目依赖项中有一些字体

$ find . -name *.ttf*
./venv/lib/python3.7/site-packages/werkzeug/debug/shared/ubuntu.ttf
./venv/lib/python3.7/site-packages/reportlab/fonts/Vera.ttf
./venv/lib/python3.7/site-packages/reportlab/fonts/VeraBI.ttf
./venv/lib/python3.7/site-packages/reportlab/fonts/VeraBd.ttf
./venv/lib/python3.7/site-packages/reportlab/fonts/VeraIt.ttf

所以我像这样通过了维拉

font = ImageFont.truetype(r'./venv/lib/python3.7/site-packages/reportlab/fonts/Vera.ttf', 50)

您也可以得到这样的字体,但是字体太小

font = ImageFont.load_default()

答案 5 :(得分:0)

在 Windows 10 中使用 Visual 代码时,我必须执行以下操作才能使其工作。

font = ImageFont.truetype(os.environ['LOCALAPPDATA'] + "/Microsoft/Windows/Fonts/Dance Floor.ttf", 10)