The Wayback Machine - http://web.archive.org/web/20240616172745/https://www.geeksforgeeks.org/python-opencv-waitkeyex-function/
Open In App

Python OpenCV – waitKeyEx() Function

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 windows needs to destroyed. If given 0 it waits for infinite till any key is pressed to destroy window.

Return : This method return the full key code of the key which is pressed. If no key is pressed it return -1.

Example 1: 

In the below example we have implemented the waitKeyEx() method we have made a window that has an image named “gfg_logo.png” and then we display it and using waitKeyEx() method we delay the closing of the window and then press a key to close it. We store the returned value in the full_key_code variable and print it.

Python




# importing cv2 module
import cv2
  
# read the image
img = cv2.imread("gfg_logo.png")
  
# showing the image
cv2.imshow('gfg', img)
  
# waiting using waitKeyEX method and storing
# the returned value in full_key_code
full_key_code = cv2.waitKeyEx(0)
  
# printing the variable
print("The key code is:"+str(full_key_code))


Output:

The key code is:13

In the output, the value of full_key_code will be printed according to the key pressed When we press enter the value that is printed is as follows.

Example 2:

Another Example we can see is where we don’t press any key and wait for the window to destroy automatically after the delay that is given. We will pass 5000 as a parameter to wait for 5 seconds and then a window will close automatically without the need of pressing any key. In this case the function will return -1 as no key was pressed.

Python




# importing cv2 module
import cv2
  
# read the image
img = cv2.imread("gfg_logo.png")
  
# showing the image
cv2.imshow('gfg', img)
  
# waiting using waitKeyEX method and
# storing the returned value in full_key_code
full_key_code = cv2.waitKeyEx(5000)
  
# printing the variable
print("The key code is:"+str(full_key_code))


Output:

The key code is:-1


Similar Reads

OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 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 nametitle: title we want to set for the window with the abo
1 min read
Python OpenCV - imencode() Function
Python OpenCV imencode() function converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier. Basic example of imencode() FunctionExample 1: We began by importing the necessary libraries, which are OpenCV and NumPy. After importing lib
2 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) Parameters: window_name: Name of the window that
1 min read
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
1 min read
Python OpenCV - getRotationMatrix2D() Function
cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image. Syntax: cv2.getRotationMatrix2D(center, angle, scale) Parameters: center: Center of rotationangle(θ): Angle of Rotation. Angle is positive for anti-clockwise and negative for clockwise.scale: scaling factor which scales the image
3 min read
Python OpenCV - destroyAllWindows() Function
Python Opencv destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. If you have multiple windows open at the same time and you want to close then you would use this function. It doesn't take any parameters and doesn't return anything. It is similar to destroyWindow() function but this functi
2 min read
Article Tags :
Practice Tags :