cv2 waitkey in Python Example : Display an Image for Specific Time

cv2 waitkey in Python Example

cv2 waitkey() allows you to wait for a specific time in milliseconds until you press any button on the keyword. It accepts time in milliseconds as an argument. In this entire tutorial, you will know the implementation of the cv2 waitkey() method in python.

How does cv2.waitkey() Work

The waitkey() is a keyword binding function and it only accepts time in milliseconds as an argument. When you add any time as an argument, then it waits for the specified time and then the program continues.

If o is passed, it waits indefinitely until a key is pressed. It can also be useful in determining the keyboard alphabet you have typed. In the next section, we will know the steps to implement it on an image.

Steps to implement cv2 waitkey

Step 1: Import the necessary library

In my example, I m using only the OpenCV module, so let’s import it using the import statement.

import cv2

Step 2: Read the image

In OpenCV to read the image, you have to use the cv2.imread() method. I am reading the following image.

flowers
Sample Image for using cv2.waitkey() method.

Let’s read the image and know the dimension of this image.

# read the image
img = cv2.imread("flowers.jpg")

To know the pixels or dimensions of the image use img.shape. 

print(img.shape)
(425, 640, 3)

Step 3: Use cv2.waitkey() and dislay the image

Now after reading the image let’s display the image on the window screen. To display the image you have to use the method cv2.imshow() in combination with waitkey(). There are many cases on it. I will discuss all of them one by one.

Case 1: Show the image for 3 seconds

To display the image for 3 seconds you have to pass 3000 (in milliseconds) as an argument to the cv2.waitkey() method. The window will automatically close after the 3 seconds.

Execute the below-given code below.

import cv2
import time

img = cv2.imread("flowers.jpg")
cv2.imshow("Flowers",img)
initial_time = time.time()
cv2.waitKey(3000)
final_time = time.time()
print("Window is closed after",(final_time-initial_time))

Output

Displaying the image for three seconds
Displaying the image for three seconds

I have also used the time module to verify the running time for the waitkey() method.

initial_time = time.time()
cv2.waitKey(3000)
final_time = time.time()
print("Window is closed after",(final_time-initial_time))

Output

Window is closed after 3.0099525451660156

Case 2: Displaying  the image for an indefinite time

There is also a case when you want to display the image as long as you want. To do so you have to pass 0 as an argument inside the cv2.waitkey() method. The window will be closed after any key is pressed.

import cv2
import time

img = cv2.imread("flowers.jpg")
cv2.imshow("Flowers",img)
initial_time = time.time()
cv2.waitKey(0)
final_time = time.time()
print("Window is closed after",(final_time-initial_time))

Output

Window is closed after 7.583725929260254

In my example, I have pressed the key after approx 7 seconds. After pressing the window is automatically closed.

These were the cases I have compiled for you. Hope it has cleared your queries on how to use waitkey() method in python. Even if you have any doubts then you can contact us for more help.

Other Questions

 

Question: I am getting no module named cv2 error

To fix this no module named named cv2 error you have to properly install the opencv module. To do so you have to use the following command on your terminal.

pip install opencv-python

It will successfully install the opencv package and you will not get the error again.

 

Question : You are getting cv2.waitKey() error

Sometimes when you use cv2.waitKey() you can get the error “AttributeError: ‘module’ object has no attribute ‘waitkey'”. This error comes when you are misspelling the waitKey() function. You are calling the cv2.waitkey() method instead of waitKey(). Please note that alphabet “k” is capital in cv2.waitKey() function.

Now you will run the code then you will not get this attribute error.

Similar Post –

cv2 addweighted Implementation: Example with Steps

Flip an Image in Python using cv2 flip method with Examples

cv2 erode method Implementation in Python with Steps

package cv2 normalize Method Implementation in Python

cv2 imdecode method Implementation in Python : With Steps

 

Source:

OpenCV Offical Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner