The cv2.imshow command does not work properly in opencv-python

I am using opencv 2.4.2, python 2.7 The following simple code created a window with the correct name, but its contents are simply empty and do not display the image:

import cv2 img=cv2.imread('C:/Python27/03323_HD.jpg') cv2.imshow('ImageWindow',img) 

Does anyone know about this issue?

+76
python image-processing opencv
Feb 16 '14 at 11:24
source share
13 answers

imshow() only works with waitKey() :

 import cv2 img = cv2.imread('C:/Python27/03323_HD.jpg') cv2.imshow('ImageWindow', img) cv2.waitKey() 

(The entire message loop needed to update the window is hidden there.)

+166
Feb 16 '14 at 11:27
source share
โ€” -

I found the answer that worked for me here: http://txt.arboreus.com/2012/07/11/highgui-opencv-window-from-ipython.html

If you start an ipython interactive session and want to use highgui windows, do cv2.startWindowThread () first.

Read more: HighGUI is a simplified interface for displaying images and videos from OpenCV code. It should be so simple:

 import cv2 img = cv2.imread("image.jpg") cv2.startWindowThread() cv2.namedWindow("preview") cv2.imshow("preview", img) 
+31
Apr 11 '15 at 23:07 on
source share

You should use cv2.waitKey(0) after cv2.imshow("window",img) . Only then will it work.

 import cv2 img=cv2.imread('C:/Python27/03323_HD.jpg') cv2.imshow('Window',img) cv2.waitKey(0) 
+19
Oct 28 '15 at 11:08
source share

I ran into the same problem. I tried to read the image from IDLE and tried to display it using cv2.imshow() , but the viewport freezes and shows that pythonw.exe not responding when trying to close the window.

The message below provides a possible explanation of why this is happening.

pythonw.exe is not responding

"In principle, do not do this from IDLE. Write a script and run it from a shell or script directly if on Windows, calling it the extension .pyw and double-clicking on it. There seems to be a conflict between the IDLE's own event loop and graphical tools GUI interfaces.

When I used imshow() in a script and ran it, rather than running it directly through IDLE, it worked.

+7
Jun 11
source share

If you are working inside the Python console, follow these steps:

 img = cv2.imread("yourimage.jpg") cv2.imshow("img", img); cv2.waitKey(0); cv2.destroyAllWindows() 

Then, if you press Enter on the image, it will successfully close the image, and you can continue to execute other commands.

+5
Aug 10 '18 at 3:00
source share

add cv2.waitKey(0) at the end.

+4
Jun 20 '18 at 11:32
source share

I had a similar problem and it was solved by uninstall

 import gtk, pygtk # remove these 

I use Linux, so it may be that gtk will not work. I was going to use it to get the screen size, to put the two images shown next to each other, but I guess I need to do a different way to do this. Hardcoded is 1920x1080 at the moment ..

+3
Jan 17 '17 at 12:57
source share

You have all the necessary fragments in this thread:

 if cv2.waitKey(): cv2.destroyAllWindows() 

works great for me in IDLE.

+3
Apr 21 '17 at 20:05
source share

If you decide to use "cv2.waitKey (0)", make sure you write "cv2.waitKey (0)" instead of "cv2.waitkey (0)" because this lowercase "k" may freeze your program too.

+2
Jul 24 '17 at 14:55
source share

If you havenโ€™t done this, better put

 import cv2 img=cv2.imread('C:/Python27/03323_HD.jpg') cv2.imshow('Window',img) cv2.waitKey(0) 

into one file and run it.

+1
Jul 07 '17 at 18:27
source share

I have waitKey () with a number greater than 0

  cv2.waitKey(1) 
0
Apr 7 '19 at 17:36
source share

I used openCV with pyplot and was able to get the image to render in the current jupyter window using pyplots imshow and show.

  import matplotlib.pyplot as plt import matplotlib.image as mpimg # First, we need to read in an image image = mpimg.imread('my_image.jpg') plt.imshow(image) # then using openCV algorithms on the image, eg grey scale import cv2 gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) #grayscale conversion # finally output the result through pyplot in the current window plt.imshow(gray, cmap='gray') plt.show() 
-one
Sep 25 '17 at 17:21
source share

error: (-215) size.width> 0 && size.height> 0 in the imshow function

This error is caused because the image was not found. So this is not an imshow function error.

-one
Mar 30 '18 at 14:04
source share



All Articles