1

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

5
  • Rather than saying "it doesn't print null", please say what it does print. Please also try making/finding a different shape image and seeing if it gets the dimensions correct. Commented Jul 28, 2020 at 16:19
  • It gives me a long nested list of matrices which I assume are the traversed values after processing the image Commented Jul 28, 2020 at 16:59
  • No, if you do print(img.shape) as in your code, you should get something like (480,640,3) Commented Jul 28, 2020 at 17:01
  • Now that I have executed it again, it gives me (1446, 698, 3). Are the height and width dimensions a problem? Commented Jul 30, 2020 at 13:24
  • 1
    Ok, that looks reasonable for your image. Now grab a small image from the Internet, or make a small screen-grab and, AFTER COPYING AND SAVING YOUR EXISTING IMAGE, overwrite the existing image with the smaller one and see if it works and the dimensions change to match the smaller image. This will prove your program is indeed looking at the correct image. Commented Jul 30, 2020 at 13:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.