ImageFont IO error: cannot open resource

I am new to Python and tried to run the following code. I got the following error "IOError: cannot open resource" . Is this because some of the image features no longer exist (e.g. Coval.otf), or is it potentially due to write / read restrictions? please let me know - thank you very much, 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) 
+13
python io python-imaging-library
source share
5 answers

This is because Coval.otf cannot be read, possibly because it does not exist on your system, it is indicated in the ImageFont doc , I tried looking for a specific font and did not find a way to get it . Take a look at the @ NewYork167 link if you should use the Coval font.

In any case, in order to save yourself from the need to install fonts, you can simply change the font call that exists on your system, use the one specified in the example documents:

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

For me, after starting the following:

 conda install -c conda-forge graphviz conda install -c conda-forge python-graphviz 

and then associate the font on Mac with:

  img = Image.open("tree1.png") draw = ImageDraw.Draw(img) font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15) 

It worked perfectly.

+4
source share

It looks like you can install Coval here to save you from having to change fonts in future code https://fontlibrary.org/en/font/bretan

+1
source share

If you use colab, you will need to specify the path correctly, just writing arial.ttf is not enough. To get the path if this font type is available in colab:! Fc-list or ! Fc-list | grep "" and then you can add all the way. enter image description here

0
source share

Font files for PIL on Windows are case sensitive. Go to Windows / Fonts:

Some fonts * .tff

Others * .TFF

You should use the actual file name, not the font header that Windows displays from the control panel.

0
source share

All Articles