use*_*116 13 python io python-imaging-library
我是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)
Run Code Online (Sandbox Code Playgroud)
Jim*_*ard 15
这是因为Coval.otf无法读取,可能是因为它在您的系统上不存在,这是在ImageFont doc.我试着搜索特定的字体,发现无法获取它.如果必须使用该字体,请查看@ NewYork167的链接. Coval
无论哪种方式,为了省去安装字体的麻烦,您只需将调用更改为系统中存在的字体,使用文档示例中指定的字体:
font = ImageFont.truetype("arial.ttf", 15)
Run Code Online (Sandbox Code Playgroud)
小智 5
对我来说,运行以下命令后:
conda install -c conda-forge graphviz
conda install -c conda-forge python-graphviz
Run Code Online (Sandbox Code Playgroud)
然后在 mac 上链接字体:
img = Image.open("tree1.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
Run Code Online (Sandbox Code Playgroud)
它工作得很好。