简体   繁体   English

ImageFont IO 错误:无法打开资源

[英]ImageFont IO error: cannot open resource

I am new to Python and tried to run the following code.我是 Python 新手并尝试运行以下代码。 I received the following error "IOError: cannot open resource" .我收到以下错误"IOError: cannot open resource" Is this due to the fact that some of the Image characteristics do not longer exist (eg Coval.otf), or is it potentially due to writing/reading restrictions?这是因为某些图像特征不再存在(例如 Coval.otf),还是可能是由于写入/读取限制? please let me know - many thanks, W请让我知道 - 非常感谢,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)

It's because Coval.otf cannot be read, probably because it doesn't exist on your system, this is specified in the ImageFont doc .这是因为无法读取Coval.otf ,可能是因为它在您的系统上不存在,这在ImageFont doc指定。 I tried searching for the specific font and found no way of aquiring it . 我尝试搜索特定字体,但没有找到获取它的方法 Look at @NewYork167's link if you must use the Coval font.如果您必须使用Coval字体,请查看 @NewYork167 的链接

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 example of the docs:无论哪种方式,为了避免安装字体的麻烦,您只需将调用更改为系统上存在的字体,使用文档示例中指定的字体:

font = ImageFont.truetype("arial.ttf", 15)

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.它工作得很好。

If you are using colab then you will have to provide path properly just writing arial.ttf is not sufficient.如果您使用的是 colab,那么您必须正确提供路径,仅编写 arial.ttf 是不够的。 To get the path if that font-type is available on colab : !fc-list or !fc-list |如果该字体类型在 colab 上可用,要获取路径: !fc-list!fc-list | grep "" and then you can add the whole path. grep ""然后你可以添加整个路径。 enter image description here在此处输入图片说明

看起来您可以从这里安装 Coval,以免您在以后的代码中也不必更改字体https://fontlibrary.org/en/font/bretan

The font files for PIL on windows are case sensitive. Windows 上的 PIL 字体文件区分大小写。 Go to Windows/fonts:转到 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 从控制面板显示的字体标题。

I also found that for Anaconda3 2019.03 the truetype font var is case sensitive.我还发现,对于 Anaconda3 2019.03,truetype 字体 var 区分大小写。 I'm using Windows 10, and had to look in C:\\Windows\\Fonts.我使用的是 Windows 10,不得不查看 C:\\Windows\\Fonts。 Looking at the properties I saw the 'Arial.ttf' font was 'arial.ttf' in the explorer.查看属性,我在资源管理器中看到“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.烦人的变化,但对我有用。

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这样的相对路径不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM