Python ImageFont IO 错误:无法打开资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33544897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImageFont IO error: cannot open resource
提问by user1885116
I am new to Python and tried to run the following code. I received the following error "IOError: cannot open resource"
. Is this due to the fact that some of the Image characteristics do not longer exist (e.g. Coval.otf), or is it potentially due to writing/reading restrictions? please let me know - many thanks, W
我是 Python 新手并尝试运行以下代码。我收到以下错误"IOError: cannot open resource"
。这是因为某些图像特征不再存在(例如 Coval.otf),还是可能是由于写入/读取限制?请让我知道 - 非常感谢,W
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from skimage import transform as tf
def create_captcha(text, shear=0, size=(100,24)):
im = Image.new("L", size, "black")
draw = ImageDraw.Draw(im)
font = ImageFont.truetype(r"Coval.otf", 22)
draw.text((2, 2), text, fill=1, font=font)
image = np.array(im)
affine_tf = tf.AffineTransform(shear=shear)
image = tf.warp(image, affine_tf)
return image / image.max()
%matplotlib inline
from matplotlib import pyplot as plt
image = create_captcha("GENE", shear=0.5)
采纳答案by Dimitris Fasarakis Hilliard
It's because Coval.otf
cannot be read, probably because it doesn't exist on your system, this is specified in the ImageFont doc
. I tried searching for the specific font and found no way of aquiring it. Look at @NewYork167's linkif you mustuse the Coval
font.
这是因为Coval.otf
无法读取,可能是因为它在您的系统上不存在,这是在ImageFont doc
. 我尝试搜索特定字体,但没有找到获取它的方法。如果您必须使用该字体,请查看 @NewYork167 的链接。 Coval
Either way, to save yourself the trouble of installing fonts, you could just change the call to a font that exists on your system, use the one specified in the exampleof the docs:
无论哪种方式,为了避免安装字体的麻烦,您只需将调用更改为系统上存在的字体,使用文档示例中指定的字体:
font = ImageFont.truetype("arial.ttf", 15)
回答by Newyork167
Looks like you can install Coval from here to save you from having to change fonts in future code as well https://fontlibrary.org/en/font/bretan
看起来您可以从这里安装 Coval,以免您在以后的代码中也不必更改字体 https://fontlibrary.org/en/font/bretan
回答by Eklil Khan
For me after running the following:
运行以下命令后对我来说:
conda install -c conda-forge graphviz
conda install -c conda-forge python-graphviz
and then linking the font on mac by:
然后通过以下方式链接 mac 上的字体:
img = Image.open("tree1.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
It worked perfectly.
它工作得很好。
回答by Siddhant Sinha
If you are using colab then you will have to provide path properly just writing arial.ttf is not sufficient. To get the path if that font-type is available on colab : !fc-listor !fc-list | grep ""and then you can add the whole path.enter image description here
如果您使用的是 colab,那么您必须正确提供路径,仅编写 arial.ttf 是不够的。如果该字体类型在 colab 上可用,要获取路径: !fc-list或!fc-list | grep ""然后你可以添加整个路径。在此处输入图片说明
回答by Steve Watt
The font files for PIL on windows are case sensitive. Go to Windows/fonts:
Windows 上的 PIL 字体文件区分大小写。转到 Windows/字体:
Some fonts are *.tff
有些字体是 *.tff
Others are *.TFF
其他是*.TFF
You have to use the actual file name and not the font title thingy that Windows shows from control panel.
您必须使用实际的文件名,而不是 Windows 从控制面板显示的字体标题。
回答by Cody
I also found that for Anaconda3 2019.03 the truetype font var is case sensitive. I'm using Windows 10, and had to look in C:\Windows\Fonts. Looking at the properties I saw the 'Arial.ttf' font was 'arial.ttf' in the explorer.
我还发现,对于 Anaconda3 2019.03,truetype 字体 var 区分大小写。我使用的是 Windows 10,不得不查看 C:\Windows\Fonts。查看属性,我在资源管理器中看到“Arial.ttf”字体是“arial.ttf”。
ImageFont.truetype('arial.ttf') works while ImageFont.truetype('Arial.ttf') throws a 'cannot open resource' error.
ImageFont.truetype('arial.ttf') 起作用,而 ImageFont.truetype('Arial.ttf') 抛出“无法打开资源”错误。
Annoying change, but worked for me.
烦人的变化,但对我有用。
回答by Simon Lau
In my case (Centos, Python 3.6.6), the font requires absolute path like:
在我的情况下(Centos,Python 3.6.6),字体需要绝对路径,如:
ttfont = ImageFont.truetype('/root/pyscripts/arial.ttf',35)
The relative path like ~/pyscripts/arial.ttf
won't work.
像这样的相对路径~/pyscripts/arial.ttf
不起作用。