Aditya Goyal
Aditya Goyal

Reputation: 39

OpenCV not showing image in python

This is the code that I am using for OpenCV to display image. It only shows me a blank screen instead of showing a picture.

import cv2

# location and name of file is completely correct
img = cv2.imread("./Resources/img-2.jpg")

# Doesn't give a null so its okay
print(img.shape)

# suspecting that problem is here
cv2.imshow("preview", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

The image is stored in the right location and when I'm using a similar approach for a video and a webcam, it works perfectly. The following is what the out is -

Output image

Upvotes: 1

Views: 1314

Answers (1)

RandomGuy
RandomGuy

Reputation: 1207

Try using matplotlib instead :

import matplotlib.pyplot as plt
import cv2

img = cv2.imread("./Resources/img-2.jpg")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert img pixels to RGB format, so that matplotlib displays the image properly

plt.imshow(img)
plt.show()

If it still gives you a blank image, then the problem might come from your file or filename.

Upvotes: 1

Related Questions