# PIL.UnidentifiedImageError: cannot identify image file
The Pillow Image.open() "PIL.UnidentifiedImageError: cannot identify imagefile" 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
Copied!
# ⛔️ Incorrect import statementimport Image
With the following import statement:
main.py
Copied!
# ✅ Correct import statementfrom PIL import Image
Here is an example of correctly using the Image.open()
method.
main.py
Copied!
from PIL import Imageim = Image.open('house.webp')im.show()
The code sample assumes that you have the following house.webp
image in thesame directory as your Python script.
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
.
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
Copied!
from PIL import Imagewith 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 automaticallyclosed once we're done.
The "IOError: cannot identify image file" error is often caused when the imageyou're trying to open is locked by a different process.
Using the with
statement often helps because the image file handler isautomatically 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 fileincorrectly.
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
Copied!
# on Windowsimage_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 araw string.
Raw strings treat backslashes as literal characters instead of escape characterswhich is exactly what we want.
You can also use a second backslash to escape the first.
main.py
Copied!
# On Windowsimage_path = 'C:\\Users\\bobby_hadz\\Desktop\\thumbnail.webp'
You can also use forward slashes as path component separators.
main.py
Copied!
# On Windowsimage_path = 'C:/Users/bobby_hadz/Desktop/thumbnail.webp'
However, make sure you don't have single backslashes as path componentseparators without prefixing the string with an r
.
Here is an example of an absolute path on macOS and Linux.
main.py
Copied!
# On macOS and Linuximage_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 yourPython script, you'd use the following path.
main.py
Copied!
image_path = 'house.webp'
Suppose you have the following folder structure.
shell
Copied!
my-project/ └── main.py └── images/ └── house.webp
Then, you'd use the following path.
main.py
Copied!
image_path = 'images/house.webp'# Orimage_path = './images/house.webp'
Suppose you have the following folder structure.
shell
Copied!
my-project/ └── src/ └── main.py └── house.webp
Then, you'd use the following image path.
main.py
Copied!
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 withoutclosing it.
Make sure multiple parts of your code aren't trying to interact with the sameimage 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
Copied!
from PIL import Imageim = Image.open('house.webp')im.show()im.close()
You can also try to use the with
statement which automatically closes thefile.
main.py
Copied!
from PIL import Imagewith 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 toupgrade your version of the Pillow package.
Open your terminal in your project's root directory and run the followingcommand.
shell
Copied!
pip install Pillow --upgrade# Or with pip3pip3 install Pillow --upgrade
The --upgrade option upgrades thespecified package to the newest available version.
If the error persists, the image you are trying to open might be corrupted.
Try to open a different image using the
Image.open()
method and check if itworks.If you still get the error, make sure the image you are trying to open is inone of the supported by Pillow formats.
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 identifyimage 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 followingtutorials:
- How to show a PIL Image in Jupyter Notebook
- Error executing Jupyter command 'notebook': [Errno 2] No such file or directory
- How to check your Python version in Jupyter Notebook
- Error executing Jupyter command 'notebook': [Errno 2] No such file or directory
- OSError: cannot write mode RGBA as JPEG Pillow error [Fix]
- ValueError: assignment destination is read-only [Solved]
- Convert an Image to a Base64-encoded String in Python