cv2.imshow() function is opening a window that always says not responding - python opencv

You missed one more line:

cv2.waitKey(0)

Then the window shows the image until you press any key on keyboard. Or you can pass as following:

cv2.waitKey(1000)
cv2.destroyAllWindows()

Here, window shows image for 1000 ms, or 1 second. After that, the window would disappear itself. But in some cases, it won't. So you can forcefully destroy it using cv2.destroyAllWindows()

Please read more tutorials first : http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html


None of the answers here worked in MacOS. The following works:

Just add a cv2.waitKey(1) after cv2.destroyAllWindows().

Example:

import cv2
image = cv2.imread('my_image.jpg')
cv2.imshow('HSV image', hsv_image); cv2.waitKey(0); cv2.destroyAllWindows(); cv2.waitKey(1)

The solution that worked for me: Switch from inline graphics to auto. It worked both in Spyder and in Jupyter notebooks.

  • To change Spyder setting: Go to Tools > Preferences > IPhyton console > Graphics > Backend: Automatic (Change backend from Inline to Automatic)
  • To change Notebook setting: Enter command:

    %matplotlib auto


Some background for my case (for those who may be quick to judge):

It used to work fine: I could open an image, it would load, and it would be responsive (doesn't say "Not responding", can close, focus, etc.) Then I installed some packages and ran some demo notebooks that apparently messed up some settings (Spyder open files were reset too).

I tried adding waitKey(1) (and 0, 30, 1000, etc values too). It made the image load, at least. But the image frame was "Not Responding": didn't refresh, couldn't close, didn't come to top, etc. Had to close using cv2.destroyAllWindows().

Note that everything worked fine during the duration of waitKey. I put this in a loop that shows the same image in the same named window and waits for a few seconds. During the loop everything works fine. As soon as the loop ends, the image window is "Not responding" (which looks like a GUI thread issue). I tried using cv2.startWindowThread(), and didn't make any difference.

Finally, changing from Inline graphics to Auto brought everything back to order.