PIL问题,OSError:无法打开资源 [英] PIL Issue, OSError: cannot open resource

查看:109
本文介绍了PIL问题,OSError:无法打开资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将文本放置到图像上的程序,我试图让自己陷入PIL并遇到错误:OSError:无法打开资源.这是我的第一个python程序,因此如果错误明显,我们深表歉意.

I'm attempting to write a program that places text onto an image, I'm trying to get my head round PIL and have run into the error: OSError: cannot open resource. This is my first python program so apologies if the error is obvious.

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

推荐答案

我在Windows 10 Pro(带有PIL 5.3.0)上也遇到了此问题.

I have also met this issue on Windows 10 Pro with PIL 5.3.0.

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

On my machine, the error is caused by non-ASCII font file names. If I change the the font name to only contain ASCII characters, I can open the font without any error.

编辑(2019-07-29):这确实是此问题中修复拉取请求.

Edit (2019-07-29): this is indeed a bug with ImageFont.truetype() method and it has been fixed in this pull request.

这篇关于PIL问题,OSError:无法打开资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆