Sunday, November 17, 2024
Google search engine
HomeLanguagesPython OpenCV – waitKey() Function

Python OpenCV – waitKey() Function

waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. 

Examples 1: Display image with a time limit

Using waitKey() method we show the image for 5 seconds before it automatically closes. The code will be as follows:

Python




# importing cv2 module
import cv2
  
# read the image
img = cv2.imread("gfg_logo.png")
  
# showing the image
cv2.imshow('gfg', img)
  
# waiting using waitKey method
cv2.waitKey(5000)


Output:

Example 2: Display image until key pressed

Now we can see one example of passing 0 as the parameter. This time instead of automatically closing the window would wait till any key is pressed. The code will be:

Python




# importing cv2 module
import cv2
  
# read the image
img = cv2.imread("gfg_logo.png")
  
# showing the image
cv2.imshow('gfg', img)
  
# waiting using waitKey method
cv2.waitKey(0)


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments