Python cv2 imshow: How to Show Image in Python

OpenCV-Python is the library of Python bindings designed to solve computer vision problems. It provides a cv2 module that helps us edit or save the image in the particular filesystem.

Let’s install the Python OpenCV library in Python.

Install Python OpenCV

To work with OpenCV in Python, we have to install the opencv-python module.

Type the following command.

python3 -m pip install opencv-python

# OR

pip install opencv-python

To use opencv-python in our project, we must import the cv2 module into the file.

import cv2


The cv2.imshow() method displays an image in a window. The window automatically fits the image size. To display an image, read an image with an imread() function and then call the imshow() method of the cv2 module.

The imshow() function will display the image in a window, receiving the name of the window and the image as input.

In Computer Vision applications, images are integral to the development process. Often there would be a requirement to read images and display them if required.

To read and display images using OpenCV Python, you could use cv2.imread() for reading the image to a variable and cv2.imshow() to display the image in a separate window.


cv2.imshow(window_name, image)


window_name: A string representing a window’s name in which the image is to be displayed.

image: It is an image that is to be displayed.

Return Value

The imshow() method doesn’t return anything.


See the following code.

# importing cv2
import cv2

# image path
path = './forest.jpg'

# Reading an image in default mode
image = cv2.imread(path)

# Window name in which image is displayed
window_name = 'image'

# Using cv2.imshow() method
# Displaying the image
cv2.imshow(window_name, image)

# waits for user to press any key
# (this is necessary to avoid Python kernel form crashing)

# closing all open windows

In this example, first, we are importing the cv2 module.

In the next step, we have defined an image path.

Then read the image using cv2.imread() function.

Then we have defined a window_name that we have set to image.

Then we use the imshow() function and pass the two parameters to open the window and see the image.

To keep the image window, we have used the waitKey() function.

We passed the 0 to waitKey() method, which means it will remain open forever until we said otherwise.

That is it for the Python cv2.imshow() function.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.