Python OpenCV - waitKey() Function Last Updated : 14 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Example: Python import cv2 img = cv2.imread("gfg_logo.png") cv2.imshow('gfg', img) cv2.waitKey(5000) cv2.destroyAllWindows() OutputExplanation:cv2.imread("gfg_logo.png") loads the image from file into a variable (img) as a NumPy array.cv2.imshow('gfg', img) displays the loaded image in a window titled "gfg".cv2.waitKey(5000) + cv2.destroyAllWindows() waits for 5 seconds, then closes the image window automatically.Syntaxcv2.waitKey([delay])Parameter: delay (Optional) is the time in milliseconds to wait for a key event. If 0, waits indefinitely.Returns:Returns the ASCII code of the pressed key.If no key is pressed during the delay, it returns -1.Use cases of waitkey()Let's clearly understand the use cases of cv2.waitKey() in OpenCV.1. Wait indefinitely until key press: cv2.waitKey(0) pauses the program until a key is pressed, commonly used when displaying static images to keep the window open until user input.cv2.waitKey(0) 2. Wait for a key for limited time: cv2.waitKey(milliseconds) waits for the specified time (in ms) and proceeds if no key is pressed and ideal for video playback, animations or slideshows.cv2.waitKey(1000) ExamplesExample 1: This example loads and displays an image using OpenCV and waits indefinitely until the user presses any key. Python import cv2 img = cv2.imread("gfg_logo.png") cv2.imshow('gfg', img) cv2.waitKey(0) OutputExplanation:cv2.imread() loads image into img and cv2.imshow() displays the image in a window.cv2.waitKey(0) waits indefinitely for key press and cv2.destroyAllWindows() closes the window.Example 2: This example plays a video (video.mp4) frame by frame in a loop using OpenCV. It displays each frame in real-time and allows the user to exit the playback by pressing the 'q' key. Python import cv2 cap = cv2.VideoCapture('video.mp4') while True: ret, frame = cap.read() if not ret: break cv2.imshow('Video Playback', frame) # wait 25ms and check if 'q' is pressed to quit if cv2.waitKey(25) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() Output Explanation:cv2.VideoCapture() + cap.read() opens the video and reads frames one by one in a loop.cv2.imshow() + cv2.waitKey(25) displays each frame and waits 25ms between frames (for real-time playback).Exits if 'q' is pressed, then releases the video and closes all windows. Comment More infoAdvertise with us Next Article Python OpenCV - waitKeyEx() Function Y yashgupta0524 Follow Improve Article Tags : Python OpenCV Python-OpenCV Practice Tags : python Similar Reads Python OpenCV - waitKeyEx() Function Python OpenCv waitKeyEx() method is similar to waitKey() method but it also returns the full key code. The key code which is returned is implementation-specific and depends on the used backend: QT/GTK/Win32/etc. Syntax: cv2.waitKey(delay) Parameters: delay: The time in milliseconds after which windo 2 min read Python OpenCV - setWindowTitle() Function Python OpenCV setWindowTitle() method used for giving the title of the windows. It takes 2 parameters that are windows name and the title that needs to be given. Both the parameters are expected to be of string type. Syntax: cv2.setWindowTitle( winname, title )Â Parameters: winname: windows nametitl 1 min read Python OpenCV - startWindowThread() Function This article will discuss how to use the python OpenCV startWindowThread() function. Do you want to display images and videos using a simplified interface through an OpenCV code? Then, you must check out the OpenCV startWindowsThread() function, which lets you use the high GUI windows, i.e., a simpl 3 min read Python OpenCV - resizeWindow() Function resizeWindow() method in Python OpenCV is used to resize window displaying images/videos to a specific size. The specified window size is for images excluding toolbars. This only works for created windows having flags other than CV_WINDOW_AUTOSIZE. Syntax: cv2.resizeWindow(window_name, width, height 1 min read Python OpenCV - selectroi() Function In this article, we are going to see an interesting application of the OpenCV library, which is selectROI(). With this method, we can select a range of interest in an image manually by selecting the area on the image. Syntax:Â cv2.selectROI(Window_name, source image) Parameter: window_name: Â name of 3 min read Python OpenCV - moveWindow() Function When we show the image using the imshow() function output window will open at the center or default position of a computer screen. Even if there are multiple image windows all windows will be displayed at the same position and we have to move windows manually. If we want to show image windows at a s 2 min read Like