How to Read and Show Image in Python using CV2 (OpenCV)

You can read and show an Image in Python using CV2 (OpenCV) by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.

Step 1

Import the OpenCV library. If OpenCV is not installed in your system then first install it using This Method.

import cv2 
#cv2 is used for OpenCV library

Step 2

Now read the image from the location. In my case “F:\\AiHints” is the location and “top30.png” is the name of the image. Change it according to your image location and name.

image = cv2.imread("F:\\AiHints\\top30.png")
#imread is used to read an image from a location

Step 3

This code will print the image in arrays.

print(image)
#print command is used to show an image in arrays.

Step 4

This code will show the image and “original” is only a new name of an image that will show in a window.

cv2.imshow("original",image)
#imshow command is used to show an image in a window

Step 5

waitKey() open the image for a specific time in milliseconds until you press any key. “destroyAllWindows()” will destroy all the windows that we created.

cv2.waitKey()
cv2.destroyAllWindows()

Leave a Comment

Your email address will not be published.