Python Pillow - Read Image - Image.open()


Python - Read Image using Pillow

The Pillow library provides powerful tools for working with images. In this tutorial, we will learn how to read an image using the Image.open() method of the PIL library. We will also explore different scenarios and potential errors when working with image files.


Steps to Read an Image using PIL

To read an image with the Python Pillow library, follow these steps:

  1. Import the Image class from the PIL library.
  2. Use the Image.open() method and pass the image file path as an argument. This will return an Image object that can be manipulated further.

Let's explore this with several examples.


Examples

1. Read an image identified by path using PIL

In this example, we will read an image using the Image.open() method from the PIL library. If the image is in the same directory as the Python script, you can provide just the image name. Otherwise, provide the complete path.

Python Program

from PIL import Image

im = Image.open("sample-image.png")

Explanation:

  1. The image is opened by passing the file name to Image.open().
  2. The method returns an Image object, which can be manipulated or displayed.

By default, if the image is in the same folder as the Python script, you only need to specify the image name.

If the image is in a different folder, provide the complete path.

Python Program (with complete path)

from PIL import Image

im = Image.open("D:/images/sample-image.png")

Explanation:

  1. This time, we specify the complete file path to ensure that the image is located correctly.
  2. Always ensure the path is correct to avoid errors.

2. Image not found - Negative scenario

In this example, we simulate a scenario where the image path is incorrect, and Image.open() throws an error.

Python Program

from PIL import Image

im = Image.open("D:/images/no-image.png")

Explanation:

  1. In this case, the image does not exist at the provided path, so the FileNotFoundError is raised.
  2. Be sure to double-check the file path when receiving this error.

Output

Traceback (most recent call last):
  File "d:/workspace/example.py", line 3, in <module>
    im = Image.open("D:/images/no-image.png")
  File "C:\Users\pythonexamplesorg\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2652, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'D:/images/no-image.png'

3. Read an image whose filename has no extension

Sometimes, an image file may not have an extension. Here, we attempt to open an image file without specifying its extension.

Python Program

from PIL import Image

im = Image.open("D:/sample")

Explanation:

  1. In this case, the image file does not have an extension.
  2. Despite that, Image.open() will still attempt to open the file and determine the image format based on the file's contents.

4. Handling Unsupported Image Formats

If you try to open an unsupported image format (such as a corrupted file or an unsupported extension), Pillow will throw an error. Here's how you can handle such cases:

Python Program

from PIL import Image

try:
    im = Image.open("D:/images/unsupported-image.xyz")
    im.show()
except IOError:
    print("Error: Unsupported file format or corrupt image.")

Explanation:

  1. We use a try-except block to catch any IOError if the image format is unsupported or the file is corrupt.
  2. The error message will provide information about the issue.

Summary

In this tutorial, we covered how to read an image using the Image.open() method from the Pillow library. We explored different scenarios, including how to handle missing or unsupported images, and how to use images with no extensions.


Python Libraries