PIL问题,OSError:无法打开资源

问题描述 投票:7回答:4

我正在尝试编写一个将文本放到图像上的程序,我正试图绕过PIL并遇到错误:OSError:无法打开资源。这是我的第一个python程序,如果错误很明显,请道歉。

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


im = Image.open("example.jpg")
font_type = ImageFont.truetype("Arial.ttf", 18)
draw = ImageDraw.Draw(im)
draw.text(xy=(50, 50), text= "Text One", fill =(255,69,0), font = font_type)
im.show()

我收到错误:

Traceback (most recent call last):
File "C:\Users\laurence.maskell\Desktop\attempt.py", line 7, in <module>
font_type = ImageFont.truetype("Arial.ttf", 18)
File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 259, in truetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 143, in __init__
self.font = core.getfont(font, size, index, encoding, 
layout_engine=layout_engine)
OSError: cannot open resource
image python-3.x fonts python-imaging-library
4个回答
5
投票

我在使用PIL 5.3.0的Windows 10 Pro上也遇到过这个问题。

在我的机器上,错误是由非ASCII字体文件名引起的。如果我将字体名称更改为仅包含ASCII字符,我可以打开字体而不会出现任何错误。

编辑(2019-07-29):这确实是a bug with ImageFont.truetype() method,它已在this pull request修复。


2
投票
from PIL import Image,ImageDraw,ImageFontim = Image.open("mak.png")
font_type = ImageFont.truetype("arial.ttf", 18)
draw = ImageDraw.Draw(im)
draw.text(xy=(120, 120), text= "download the font that you wanna use", fill =(255,69,0), font = font_type)
im.show()

它的“arial.ttf”不是“Arial.ttf”

这是link to download arial.ttf字体。


0
投票

PIL找不到指定的字体。检查它是否存在,并以完全相同的大小写命名。您也可以尝试直接在项目文件夹中复制它。


0
投票

它对我不起作用,因为没有安装字体。感谢这里给出的答案,现在就可以了。安装可以做here

© www.soinside.com 2019 - 2024. All rights reserved.