natasha
natasha

Reputation: 34

why doesn't python give me the option to close openCV window?

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

Upvotes: 0

Views: 889

Answers (2)

Anusha P
Anusha P

Reputation: 1

import numpy as np
import cv2

# Create an orange image
image_opencv = np.zeros((500, 500, 3), np.uint8)
image_opencv[:] = (0, 100, 255)  # BGR format

# Display the image
cv2.imshow('Orange Image', image_opencv)
cv2.waitKey(0)  # Wait for a key press
cv2.destroyAllWindows()  # Close the window

Instructions:

  1. Run the code in VSCode or the terminal.
  2. A window titled Orange Image will open. Press Esc to close it. Hope this helps! 😊

Upvotes: 0

Matthew Wisdom
Matthew Wisdom

Reputation: 151

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.

Upvotes: 1

Related Questions