DestroyWindow does not close window on Mac using Python and OpenCV

You need to run cv.startWindowThread() after opening the window.
I had the same issue and now this works for me.

Hope this helps for future readers. And there is also a cv2 binding (I advise to use that instead of cv).

This code works for me:

import cv2 as cv
import time

WINDOW_NAME = "win"

image = cv.imread("ela.jpg", cv.CV_LOAD_IMAGE_COLOR)
cv.namedWindow(WINDOW_NAME, cv.CV_WINDOW_AUTOSIZE)
initialtime = time.time()

cv.startWindowThread()

while (time.time() - initialtime < 5):
  print "in first while"
cv.imshow(WINDOW_NAME, image)
cv.waitKey(1000)

cv.waitKey(1)
cv.destroyAllWindows()
cv.waitKey(1)

initialtime = time.time()
while (time.time() - initialtime < 6):
    print "in second while"

The same issue happens with the C++ version, on Linux:
Trying to close OpenCV window has no effect

Leave a Comment