Skip to content

How to resolve OSError: cannot open resource when using python?

1. Purpose

In this post, I will demonstrate how to resolve the following exception or error when using Python:

Terminal window
Traceback (most recent call last):
File "generate_png_transparent_demo.py", line 43, in <module>
generate_png_transparent("./images/a.png","hello world")
File "generate_png_transparent_demo.py", line 37, in generate_png_transparent
d.text((10,25), the_text, fill=(255, 0, 0),font=get_font(the_text))
File "generate_png_transparent_demo.py", line 21, in get_font
font = ImageFont.truetype("arial.ttf", fontsize)
File "/Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PIL/ImageFont.py", line 852, in truetype
return freetype(font)
File "/Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PIL/ImageFont.py", line 849, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PIL/ImageFont.py", line 210, in __init__
font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource

2. The Environment

  • Python 3
  • macOS

3. The Code

3.1 The Project Directory Structure

Our working directory is named myscript, and this is its structure:

.
└── myscripts/
├── images/
│ ├── readme.txt
└── generate_png_transparent_demo.py

Explanation:

  • The images directory is the target directory where we will store the resulting image.
  • The generate_png_transparent_demo.py file contains the code that performs the task.

3.2 The Code That Causes the Problem

generate_png_transparent_demo.py
def get_font():
font = ImageFont.truetype("arial.ttf", fontsize)

According to the ImageFont.truetype documentation, the first parameter of the function is the font file name. However, when running the above code, the following error occurs:

...
font = ImageFont.truetype("arial.ttf", fontsize)
File "/Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PIL/ImageFont.py", line 852, in truetype
return freetype(font)
File "/Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PIL/ImageFont.py", line 849, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PIL/ImageFont.py", line 210, in __init__
font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource

The error indicates that the program cannot find the font file on the system.

3.3 The Solution

To resolve this issue, we need to specify the full path to the font file:

generate_png_transparent_demo.py
def get_font(txt, img_fraction=0.05):
font = ImageFont.truetype("/Library/fonts/Arial.ttf", fontsize)

After making this change, re-run the program, and the error should no longer occur.

3.4 Why Does This Happen?

On Windows, the Python PIL library can automatically find font files in system font directories. However, on macOS, the library cannot locate the font file by itself.

You can check for available .ttf files in these directories using the following Terminal command:

Terminal window
$ find {/System,}/Library/Fonts -name \*ttf

4. Summary

In this post, we demonstrated how to resolve the OSError: cannot open resource error when using the PIL library in Python, particularly on macOS. The key solution is to specify the full path to the font file, as the PIL library may not automatically locate fonts on macOS. This issue highlights a compatibility quirk between PIL and macOS, but it can be easily resolved with the correct path configuration.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!