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

50

我正在尝试编写一个程序,将文本放置在图像上。我正在努力理解 PIL 并遇到错误:OSError: cannot open resource。这是我的第一个 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

4
尝试在ImageFont.truetype中设置字体的完整路径,例如r"C:\Windows\Fonts\Arial.ttf" - Stanislav Ivanov
14个回答

34

我使用默认字体来解决问题。

font = ImageFont.load_default()

如果你只需要放置一些文本(对字体风格没有特殊要求),那么这可能是一个简单的解决方案。

font = ImageFont.load_default()

draw = ImageDraw.Draw(pil_img)
draw.text(( 20, 32), "text_string", (255,0,0), font=font)

20
from PIL import Image, ImageDraw, ImageFont

im = Image.open("mak.png")
font_type = ImageFont.truetype("arial.ttf", 18)
draw = ImageDraw.Draw(im)
draw.text(xy=(120, 120), text= "download font you want to use", fill=(255,69,0), font=font_type)
im.show()

字体文件名为"arial.ttf"而非"Arial.ttf"

这里是下载链接字体文件arial.ttf。


4
在下载arial.tff后,我把它移动到与脚本相同的目录下(实际上我只是在该目录中使用wget $URL),然后使用./arial.tff(这将使用该目录中的文件)代替arial.tff(系统会在你的计算机中搜索该文件),这样我成功地让程序工作了。 - Seabass77

9
我也遇到了在Windows 10 Pro上使用PIL 5.3.0时出现的这个问题。 在我的机器上,错误是由非ASCII字体文件名引起的。如果我将字体名称更改为仅包含ASCII字符,则可以无错误地打开字体。 编辑(2019-07-29):这确实是ImageFont.truetype()方法的一个错误,并且已经在此pull request中修复。

5

对于Linux,我使用了:

$ locate .ttf

/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-BI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-L.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-LI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-M.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-MI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-RI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-B.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-BI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-RI.ttf

它实际上返回了比那更多的东西!然后我使用在Stack Overflow发布的Python代码:将由locate返回的字体名称“Ubuntu-R.ttf”插入其中:
color_palette = [BLUE, GREEN, RED]
image_w=200
image_h=200
region = Rect(0, 0, image_w, image_h)
imgx, imgy = region.max.x+1, region.max.y+1
image = Image.new("RGB", (imgx, imgy), WHITE)
draw = ImageDraw.Draw(image)
vert_gradient(draw, region, gradient_color, color_palette)
#image.text((40, 80),"No Artwork",(255,255,255))
#font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 40)
#font = ImageFont.load_default()
font = ImageFont.truetype("Ubuntu-R.ttf", int(float(image_w) / 6))
draw.text((int(image_w/12), int(image_h / 2.5)), "No Artwork", \
           fill=(0,0,0), font=font)
image.show()

现在,我可以在播放音乐文件时显示一张图片了:

No Artwork.png


2

我也遇到了同样的问题,但后来发现这是斜杠的问题。所以,如果其他人也错过了这个小细节,这篇文章就是为你准备的。

我将字体放在字体文件夹中。当我尝试使用font/<font_name>.ttf导入字体时,代码无法找到它。用\替换/后,代码就可以找到字体了。


2

据我所理解,问题出在文件的位置:"Arial.ttf"

据我所知,Arial.ttf文件与程序一起,但是启动是从其他地方开始的。

在这种情况下,需要将Arial.ttf文件添加到完整路径中:

# __file__ contain full path to the py scrypt
# for python 3.9 and later 

import os
path = os.path.dirname(__file__) + '/'

im = Image.open("example.jpg")
font_type = ImageFont.truetype(path + "Arial.ttf", 18)

对于旧版本的Python,请参阅更多信息: 如何获取当前文件目录的完整路径?


这个解决方案对我有效。 - Kyamasam

1
如果您使用的是Windows系统,可以按照以下方法解决我的问题:
  • 进入C:\Windows\Fonts文件夹中的字体文件夹
  • 右键单击要使用的字体并点击“属性”
  • 进入安全选项卡并复制“对象名称:”的地址
  • 然后将上述路径 (*****) 放置在以下代码中:

ImageFont.truetype("*****", int(float(image_w) / 6))


0

我通过以下步骤简单地解决了这个问题:

  1. 在主目录下创建一个名为 fonts(.fonts)的隐藏文件夹。
  2. 从网上下载所需字体。 注意:您可以在Google上输入“font_name.tiff download”来下载任何字体。
  3. 将下载的.ttf文件复制并粘贴到.fonts文件夹中。
  4. .ttf文件的新路径复制并粘贴到代码中,如下所示。
im_show = draw_ocr(image, boxes, txts, scores, font_path='/.fonts/simfang.ttf')

这就是对我而言的工作方式。

你可以点击此链接作为视觉辅助。https://www.youtube.com/watch?v=Q5GO_glHXyQ&t=35s


0

当我在Linux遇到这个问题时,我通过以下方式解决:

  1. .ttf安装到Linux中

  2. 使用实际的.tff~中运行代码。由于某种原因,即使字体已安装并且.py在另一个目录中运行,它仍需要在主目录中有.tff


0

问题在于.ttf文件的名称与您传递给ImageFont.truetype()的文件名不匹配。

确保大小写和拼写正确。没有其他外部因素会影响从ttf文件加载字体。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接