Python Pillow – Show or Display Image

Python provides various libraries and modules that are used to perform different kinds of operations in programs. To perform operations on images such as cropping, resizing, modifying, opening, displaying, etc., the “pillow” module is used in Python. Python “pillow” is an open-source library for handling and manipulating image file formats.

This article will cover the following contents in detail using appropriate examples:

Using Pillow Module “Image.show()” Function

To show or display an image, the “Image.show()” function of the “pillow” module is used in Python. The syntax of the “Image.show()” function is shown below:

Image.show(title=None, command=None)

The “title” parameter is used to assign the title to an image, and the “command” parameter is used to show the image. The “Image.show()” function will return the assigned path image.

To access the pillow module’s functionality, we need to install it in Python. To do that, you can use the below command in the cmd or terminal window of the Python interpreter:

> pip install pillow

In the below snippet, the pillow module has been successfully installed using the pip command via the cmd terminal:

After installing the pillow module, now we can perform the following examples to show or display images.

Example 1: Show or Display Single Image

Let’s learn how to use the “Image.open()” and “Image.show()” functions of the pillow module to open and display an image:

Code:

from PIL import Image
image_val = Image.open("sample.png")
image_val.show()

In the above code:

  • The image function of the pillow module is imported into the program.
  • The “Image.open()” accepts the name of the image along with a format such as “sample.png” to open the image.
  • The “Image.show()” function shows or displays the image on the screen.

Note: In the above code scenario, we only mentioned the name of the image along with the format because the image was in the same directory as the Python file. We need to specify the complete file path to open an image in a different directory.

Output:

The above output shows the image named “sample.png” using the “Image.open()” and “Image.show()” functions.

Example 2: Show or Display Multiple Images

In the below code, multiple images are displayed using the “Image.open()” and “Image.show()” functions:

Code:

from PIL import Image
image_val = Image.open("sample.png")
image_val1 = Image.open("sample_1.png")
image_val.show()
image_val1.show()

In the above code:

  • The Image module is imported at the start of the program from the “PIL” library.
  • The “Image.open()” function is used twice to open the selected images using their names.
  • The “Image.show()” function is also used twice to show or display specific images in Python.

Output:

The above output shows the first image named “sample.png”.

The above snippet shows the second image using the “Image.show()” function.

Conclusion

To show or display an image, we can use the “Image.open()” and “Image.show()” functions of the “Pillow” module in Python. The “Image.open()” function accepts the path, file name, and image format to open the image in Python. To display or show the images in Python, the “Image.show()” function is utilized. This article presented a detailed guide on how to show or display images using various examples.