DestroyWindow does not close window on Mac using Python and OpenCV
There are a few peculiarities with the GUI in OpenCV. The destroyImage
call fails to close a window (atleast under Linux, where the default backend was Gtk+ until 2.1.0) unless waitKey
was called to pump the events. Adding a waitKey(1)
call right after destroyWindow
may work.
Even so, closing is not guaranteed; the the waitKey
function is only intercepted if a window has focus, and so if the window didn't have focus at the time you invoked destroyWindow
, chances are it'll stay visible till the next destroyWindow
call.
I'm assuming this is a behaviour that stems from Gtk+; the function didn't give me any trouble when I used it under Windows.
Python Image Window Not Closing Properly on Mac
I believe the quit is acting as an interrupt, which quits the entire python program. This is different than pressing a key, which is incorporated into the logic of the actual program and continues to completion:
cv2.waitKey(0) # waits for any key to be pressed to continue the logic of the program
# this is what actually allows you to "display" an image without it immediately disappearing.
Python, OpenCV can't close last window
Try to add some cv2.waitKey
after destroyAllWindows
:
cv2.destroyAllWindows()
for i in range(5): # maybe 5 or more
cv2.waitKey(1)
Sometimes, it works.
How can I close a cv2 window?
I couldn't quite replicate your error (I ended up crashing Python with your code), but I did come up with a fix.
while ok_flag:
(ok_flag, img) = cap.read()
cv2.imshow("CallingCamera View", img)
if cv2.waitKey(0) == 27:
ok_flag = False
cv2.destroyAllWindows()
I wasn't sure if you had a reason for setting the waitKey time to 1ms or not, for testing purposes setting it to 0 (forever) worked just the same. I guess setting it to 1ms for you would get you the most recent image? My test was run with a static image residing on my desktop. Otherwise removing the two breaks and the if not ok_flag statement seemed to iron everything out. You don't really need those since as soon as ok_flag goes to False the loop terminates.
python opencv: destroyallwindow does not work
maybe its the copy of the mentioned question but I followed the first comment and got it solved by using terminal but someone need to find and tell why jupyter causes such problem.
Related Topics
About the Pil Error -- Ioerror: Decoder Zip Not Available
Python Command Line Input in a Process
Permanently Adding a File Path to Sys.Path in Python
Python: Excluding Modules Pyinstaller
Putting a 'Cookie' in a 'Cookiejar'
Using Numpy Vectorize on Functions That Return Vectors
Remove or Replace Spaces in Column Names
Read Unicode Characters from Command-Line Arguments in Python 2.X on Windows
Python Multiple Inheritance Passing Arguments to Constructors Using Super
Python Runtimewarning: Overflow Encountered in Long Scalars
In Tensorflow, Differencebetween Session.Run() and Tensor.Eval()
Populate a Pandas Sparsedataframe from a Scipy Sparse Matrix
How to Write Code to Autocomplete Words and Sentences
A Queryset by Aggregate Field Value
How to Select Rows with One or More Nulls from a Pandas Dataframe Without Listing Columns Explicitly
Prepend a Level to a Pandas Multiindex