Home > Software engineering >  why doesn't python give me the option to close openCV window?
why doesn't python give me the option to close openCV window?

Time:10-29

I have the following code that prints the following image. Why don't I have the option to close the window (small Red Cross missing in top left corner)?

import cv2

img = cv2.imread('/Users/natashabustnes/Desktop/geeks14.png')
cv2.imshow('image', img)
cv2.waitKey(0)

enter image description here

CodePudding user response:

Your code displays window and waits for a keypress.
When you pressed a key, waitKey returned and the GUI froze because there's was no more instructions. Do something like this instead.

import cv2
img = cv2.imread('/Users/natashabustnes/Desktop/geeks14.png')
cv2.imshow('image', img)
while True:
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

This code waits until you press the 'q' button before closing. OpenCV by default does not support closing windows using the normal close button.

  • Related