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: