3

I'm trying to learn OpenCV framework on python3 and when I want to destroy an opened image, nothing happens and the only way to close the window is "Force Quit". I tried some solutions like‍ cv2.startWindowThread() but it didn't work for me. Here is my code:

import cv2
import numpy as np
import matplotlib as plt
img = cv2.imread('image.jpg')
cv2.startWindowThread()
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

versions:

python -> 3.6.0

OpenCV -> 3.3.0

macOS High Sierra -> 10.13.6


Update:

I was trying to run the code in jupyter-notebook and when I tried to run it in Pycharm I had no more problem.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
M.Saber
  • 83
  • 1
  • 4
  • 2
    Possible duplicate of [DestroyWindow does not close window on Mac using Python and OpenCV](https://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv) – Dmitrii Z. Oct 06 '18 at 19:40

1 Answers1

1

I'm not sure why by for my the following work around works:

cv2.imshow(name, img)
cv2.startWindowThread()
print(cv2.waitKey(0))
cv2.destroyAllWindows()
for i in range(2):
    cv2.waitKey(1)

based on this answer

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46