aidanjacobson
aidanjacobson

Reputation: 2391

imshow() function displays empty gray image. What do I do?

When I try to use cv2.imshow() it gives a shows the image for a second, then makes the window go gray. I can't seem to find anything about it, except that I should use cv2.waitKey(0), which I am, and also to use cv2.namedWindow(). My code:

import numpy as np
import cv2
import sys

cv2.namedWindow('img')
img = cv2.imread(sys.argv[1])
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows

Again, it shows the image for a second and then grays it out. Thanks in advance.

Upvotes: 4

Views: 6854

Answers (1)

Kwent
Kwent

Reputation: 84

  1. check your sys.argv[1], try to replace with real path (i.e. "images/1.png")
  2. destroyAllWindows() it is the function (use brackets)
  3. parameter of waitKey (0 in your example) it is delay in ms, 0 means "forever", default value of delay is 0.
  4. in fact you don't need namedWindow and destroyAllWindows here

try this:

import cv2
import sys

img = cv2.imread(sys.argv[1])
cv2.imshow('img', img)
cv2.waitKey()

Upvotes: 6

Related Questions