How can one display an image using cv2 in Python

Solution 1:

As far as I can see, you are doing it almost good. There is one thing missing:

cv2.imshow('image',img)
cv2.waitKey(0)

So probably your window appears but is closed very very fast.

Solution 2:

you can follow following code

import cv2
# read image 
image = cv2.imread('path to your image')
# show the image, provide window name first
cv2.imshow('image window', image)
# add wait key. window waits until user presses a key
cv2.waitKey(0)
# and finally destroy/close all open windows
cv2.destroyAllWindows()

I think your job is done then

Solution 3:

Since OpenCV reads images with BGR format, you'd convert it to RGB format before pass the image to pyplot

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('YOUR_FILEPATH')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()

Solution 4:

While using Jupyter Notebook this one might come in handy

import cv2
import matplotlib.pyplot as plt

# reading image
image = cv2.imread("IMAGE_PATH")

# displaying image
plt.imshow(image)
plt.show()