The Wayback Machine - https://web.archive.org/web/20220829040603/https://www.geeksforgeeks.org/python-opencv-waitkey-function/
Skip to content
Related Articles

Related Articles

Python OpenCV – waitKey() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Oct, 2021
View Discussion
Improve Article
Save Article

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:

Image

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:

Image

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!