OpenCV – Show Image – imshow()

To deal with images, Python provides an open source “OpenCV” library that assists us in performing multiple operations on images. For instance, “OpenCV” allows us to open, close, show, crop, resize images, etc. 

In this blog post, the “OpenCV” function “cv2.imshow()” is used to show or display the image that is placed in the Python working directory or from a specific directory.

This blog post will discuss the following content using numerous examples:

How to Show Images in Python Using OpenCV?

Python “OpenCV” module functions named “cv2.imshow()” are used along with the “cv2.imread()” to show images in Python. The “cv2.imread()” reads images from the specific path, and the “cv2.imshow()” function is used to show that image in Python. The basic syntax of this function is shown below:

cv2.imshow(window_name, image)

The first argument takes the image name, which appears in the popup windows when “cv2.imshow()” displays the image. The second argument takes the value of the image read by the “cv2.imread()” function.

But before going to the example of “cv2.imshow(),” we need to install the “OpenCV” library in Python using the below command.

> pip install opencv-python

The above snippet shows the installation of the “OpenCV” module in Python.

Once the “opencv” module is installed, you can use any of its functions, including the “cv2.imshow()” function.

Example 1: Showing Image Present in Current Working Directory of Python

In the below code, the “cv2.imshow()” function is used to show the image that is present in the current working directory of Python:

Code:

import cv2
img_read = cv2.imread('ILF.png')
cv2.imshow('itslinuxfoss',img_read)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code:

  • The “cv2.imread()” reads the image by accepting the name along with the image format.
  • The “cv2.imshow()” shows an image that is read by the “cv2.imread()” function by accepting the pop-up window name as a first argument and variable “img_read” as a second argument.

Output:

The image named “ILF.png” has been shown using the Python program.

Example 2: Showing Image Present at Specific Directory of Python

In this example, the image that is placed in a specific directory in the system was read by “cv2.imread()” and displayed by “cv2.imshow()”. The complete image path is shown below:

Image Path: C:\Users\p\Downloads\Joseph\ItsLinuxFoss

Code:

import cv2
img_read = cv2.imread('C:/Users/p/Downloads/Joseph/ItsLinuxFoss/ILF.png')
cv2.imshow('itslinuxfoss',img_read)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code:

  • The complete “path” is defined in the “cv2.imread()” function.
  • The “cv2.imshow()” accepts the read image as an argument and shows an image on the screen.

Output:

The Python program shows the image named “ILF.png” from the targeted directory.

Conclusion

To show an image, the “cv2.imshow()” function of the “OpenCV” module is utilized in Python. This function can display the image from the current working directory and from a specific directory. The image is read from the current Python directory or the specific path using the “cv2.imread()” function and displayed by the “cv2.imshow()” function. In this guide, we discussed how to show images using the OpenCV package.