Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageFont IO error: cannot open resource

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

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)
like image 366
user1885116 Avatar asked Nov 05 '15 12:11

user1885116


1 Answers

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 link if you must use the Coval font.

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)
like image 79
Dimitris Fasarakis Hilliard Avatar answered Sep 20 '22 23:09

Dimitris Fasarakis Hilliard