PIL.UnidentifiedImageError: cannot identify image file

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# PIL.UnidentifiedImageError: cannot identify image file

The Pillow Image.open() "PIL.UnidentifiedImageError: cannot identify image file" error occurs for multiple reasons:

  • Using an incorrect import statement. The import statement should be from PIL import Image.
  • Passing the path to the Image.open() method incorrectly.
  • The image is still being used by a different process.
  • Having an outdated version of the Pillow module.

First, make sure that your import statement is correct.

Replace the following import statement:

main.py
# ⛔️ Incorrect import statement import Image

With the following import statement:

main.py
# ✅ Correct import statement from PIL import Image

Here is an example of correctly using the Image.open() method.

main.py
from PIL import Image im = Image.open('house.webp') im.show()

The code sample assumes that you have the following house.webp image in the same directory as your Python script.

house

You can right-click on the image and select "Save image as" to download it.

Here is the output of running the file with python main.py.

using image open method correctly

The Image.open() method takes the path to the image file as a parameter.

You can also use the with statement to open the image using Pillow.

The following code sample produces the same result.

main.py
from PIL import Image with Image.open("house.png") as im: im = Image.open('house.webp') im.show()

The benefit of using the with statement is that the image is automatically closed once we're done.

The "IOError: cannot identify image file" error is often caused when the image you're trying to open is locked by a different process.

Using the with statement often helps because the image file handler is automatically closed even if an error occurs.

# Make sure the path to the image file is correct

Another common cause of the error is specifying the path to the image file incorrectly.

The path you pass to the Image.open() method can be absolute or relative.

Here is an example of an absolute path on Windows.

main.py
# on Windows image_path = r'C:\Users\bobby_hadz\Desktop\thumbnail.webp'

Notice that we prefixed the string with an r.

Make sure to prefix the path with an r to mark it as a raw string.

Raw strings treat backslashes as literal characters instead of escape characters which is exactly what we want.

You can also use a second backslash to escape the first.

main.py
# On Windows image_path = 'C:\\Users\\bobby_hadz\\Desktop\\thumbnail.webp'

You can also use forward slashes as path component separators.

main.py
# On Windows image_path = 'C:/Users/bobby_hadz/Desktop/thumbnail.webp'

However, make sure you don't have single backslashes as path component separators without prefixing the string with an r.

Here is an example of an absolute path on macOS and Linux.

main.py
# On macOS and Linux image_path = '/home/borislav/Desktop/bobbyhadz_python/thumbnail.webp'

A relative path is one that is relative to your Python script.

For example, if you have a house.webp image in the same directory as your Python script, you'd use the following path.

main.py
image_path = 'house.webp'

Suppose you have the following folder structure.

shell
my-project/ └── main.py └── images/ └── house.webp

Then, you'd use the following path.

main.py
image_path = 'images/house.webp' # Or image_path = './images/house.webp'

Suppose you have the following folder structure.

shell
my-project/ └── src/ └── main.py └── house.webp

Then, you'd use the following image path.

main.py
image_path = '../house.webp'

The ../ prefix is used to navigate one directory up.

Similarly, if you need to navigate 2 directories up, you'd use ../../.

# The image might be locked by another process

The error is also caused when the image gets locked by a different process.

For example, you might be trying to open the same image multiple times without closing it.

Make sure multiple parts of your code aren't trying to interact with the same image at the same time.

You can try to use the Image.close() method once you're done.

The method closes the file pointer if that is possible.

The operation destroys the image's core and releases its memory.

The image's data is unusable afterward.

main.py
from PIL import Image im = Image.open('house.webp') im.show() im.close()

You can also try to use the with statement which automatically closes the file.

main.py
from PIL import Image with Image.open("house.png") as im: im = Image.open('house.webp') im.show()

Once you exit the indented with block, the file is automatically closed.

# Try upgrading your Pillow version

If the error persists, try to upgrade your version of the Pillow package.

Open your terminal in your project's root directory and run the following command.

shell
pip install Pillow --upgrade # Or with pip3 pip3 install Pillow --upgrade

The --upgrade option upgrades the specified package to the newest available version.

If the error persists, the image you are trying to open might be corrupted.

  1. Try to open a different image using the Image.open() method and check if it works.

  2. If you still get the error, make sure the image you are trying to open is in one of the supported by Pillow formats.

  3. Make sure the image file is not empty (e.g. an image file with a size of 0 bytes).

# Conclusion

To solve the Pillow Image.open() "PIL.UnidentifiedImageError: cannot identify image file" error, make sure:

  • You are using the correct import statement (from PIL import Image).
  • You've passed the path to the image file correctly when calling Image.open().
  • The image is not being used by a different process.
  • Your version of the Pillow module is not out of date.
  • The image You are trying to open is supported by Pillow.
  • The image file is not empty (e.g. an image file with a size of 0 bytes).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.