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

Lau*_*ell 10 fonts image python-imaging-library python-3.x

我正在尝试编写一个将文本放到图像上的程序,我正试图绕过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()
Run Code Online (Sandbox Code Playgroud)

我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

Uzz*_*der 15

固定的使用问题默认字体

font = ImageFont.load_default()
Run Code Online (Sandbox Code Playgroud)

如果您只需要放置一些文本(字体样式对您来说无关紧要),那么这可能是一个简单的解决方案。

font = ImageFont.load_default()

draw = ImageDraw.Draw(pil_img)
draw.text(( 20, 32), "text_string", (255,0,0), font=font)
Run Code Online (Sandbox Code Playgroud)


小智 9

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()
Run Code Online (Sandbox Code Playgroud)

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

这是下载arial.ttf 字体的链接

  • 下载 arial.tff 后,我能够在将 .tff 文件移动到与脚本相同的目录后让我的程序工作(我实际上只是在目录中使用 wget $URL),然后我使用了 ./arial.tff(这个使用您目录中的那个)而不是 arial.tff (这个在您的系统中搜索文件) (2认同)

jdh*_*hao 6

我在带有PIL 5.3.0的Windows 10 Pro上也遇到了此问题。

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

编辑(2019-07-29):这确实ImageFont.truetype()method 的错误,并且已在此pull request中修复。


Win*_*nix 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
Run Code Online (Sandbox Code Playgroud)

它实际上返回的远不止于此!

然后我在 Stack Overflow 上发布了 python 代码:

插入由以下命令返回的字体名称“Ubuntu-R.ttf” locate

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()
Run Code Online (Sandbox Code Playgroud)

瞧!现在,当我正在播放的音乐文件中没有可显示的图像时,我可以显示图像:

没有艺术品.png


bla*_*box 0

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