ImageFont IO error: cannot open resource
ImageFont IO error: cannot open resource
我是 Python 的新手并尝试 运行 以下代码。我收到以下错误 "IOError: cannot open resource"
。这是因为某些图像特征不再存在(例如 Coval.otf),还是可能由于 writing/reading 限制?请告诉我 - 非常感谢,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)
是因为Coval.otf
无法读取,可能是因为你的系统不存在,如果你必须,这在ImageFont doc
. I tried searching for the specific font and found no way of aquiring it. Look at @NewYork167's link中指定使用 Coval
字体。
无论哪种方式,为了省去安装字体的麻烦,您可以将调用更改为系统上存在的字体,使用文档 example 中指定的字体:
font = ImageFont.truetype("arial.ttf", 15)
看起来你可以从这里安装 Coval,这样你就不必在以后的代码中更改字体了
https://fontlibrary.org/en/font/bretan
我在 运行 之后:
conda install -c conda-forge graphviz
conda install -c conda-forge python-graphviz
然后通过以下方式链接 mac 上的字体:
img = Image.open("tree1.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
效果很好。
如果您使用的是 colab,那么您必须正确提供路径,仅编写 arial.ttf 是不够的。
如果 font-type 在 colab 上可用,则获取路径:
!fc-list 或 !fc-list | grep ""
然后你可以添加整个路径。enter image description here
windows 上的 PIL 字体文件区分大小写。
转到 Windows/fonts:
部分字体为*.tff
其他是*.TFF
您必须使用实际的文件名,而不是 Windows 从控制面板显示的字体标题。
我还发现对于 Anaconda3 2019.03,truetype 字体 var 区分大小写。我正在使用 Windows 10,并且必须查看 C:\Windows\Fonts。查看属性,我看到 'Arial.ttf' 字体在资源管理器中是 'arial.ttf'。
ImageFont.truetype('arial.ttf') 工作时
ImageFont.truetype('Arial.ttf') 抛出 'cannot open resource' 错误。
烦人的变化,但对我有用。
在我的例子中(Centos,Python 3.6.6),字体需要绝对路径,如:
ttfont = ImageFont.truetype('/root/pyscripts/arial.ttf',35)
像~/pyscripts/arial.ttf
这样的相对路径是行不通的。
我是 Python 的新手并尝试 运行 以下代码。我收到以下错误 "IOError: cannot open resource"
。这是因为某些图像特征不再存在(例如 Coval.otf),还是可能由于 writing/reading 限制?请告诉我 - 非常感谢,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)
是因为Coval.otf
无法读取,可能是因为你的系统不存在,如果你必须,这在ImageFont doc
. I tried searching for the specific font and found no way of aquiring it. Look at @NewYork167's link中指定使用 Coval
字体。
无论哪种方式,为了省去安装字体的麻烦,您可以将调用更改为系统上存在的字体,使用文档 example 中指定的字体:
font = ImageFont.truetype("arial.ttf", 15)
看起来你可以从这里安装 Coval,这样你就不必在以后的代码中更改字体了 https://fontlibrary.org/en/font/bretan
我在 运行 之后:
conda install -c conda-forge graphviz
conda install -c conda-forge python-graphviz
然后通过以下方式链接 mac 上的字体:
img = Image.open("tree1.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
效果很好。
如果您使用的是 colab,那么您必须正确提供路径,仅编写 arial.ttf 是不够的。 如果 font-type 在 colab 上可用,则获取路径: !fc-list 或 !fc-list | grep "" 然后你可以添加整个路径。enter image description here
windows 上的 PIL 字体文件区分大小写。 转到 Windows/fonts:
部分字体为*.tff
其他是*.TFF
您必须使用实际的文件名,而不是 Windows 从控制面板显示的字体标题。
我还发现对于 Anaconda3 2019.03,truetype 字体 var 区分大小写。我正在使用 Windows 10,并且必须查看 C:\Windows\Fonts。查看属性,我看到 'Arial.ttf' 字体在资源管理器中是 'arial.ttf'。
ImageFont.truetype('arial.ttf') 工作时 ImageFont.truetype('Arial.ttf') 抛出 'cannot open resource' 错误。
烦人的变化,但对我有用。
在我的例子中(Centos,Python 3.6.6),字体需要绝对路径,如:
ttfont = ImageFont.truetype('/root/pyscripts/arial.ttf',35)
像~/pyscripts/arial.ttf
这样的相对路径是行不通的。