# What Actually WaitKey is doing

**URL:** <https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605>\
**Category:** Python\
**Tags:** highgui\
**Created:** [April 5, 2021, 7:58am UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605 "2021-04-05T07:58:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiglon](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@tiglon](https://forum.opencv.org/u/tiglon)\
**Post date:** [April 5, 2021, 7:58am UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/1 "2021-04-05T07:58:20Z")

</div>

I want to know that what actually cv2.waitKey() is doing … I know that it is used for waiting for a particular amount of milliseconds or for indefinite period of time (if 0 is mentioned) . But , when I ran the following python code first a grey window popped up(for ten seconds ) and then after 10s , the image came up.  
import cv2  
import time  
image = cv2.imread("./my.jpg")  
cv2.imshow(“Image” , image)  
time.sleep(10)  
cv2.waitKey(3000)  
So, is waitKey the one who is responsible for showing the image or some other thing is going on internally ???

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [April 5, 2021, 8:15am UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/2 "2021-04-05T08:15:38Z")

</div>

Hi,  
What’s your problem with [doc](https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7)?

---

<div class="post-metadata">

**Author:** ![FilipBaas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/filipbaas/32/802_2.png) [@FilipBaas](https://forum.opencv.org/u/FilipBaas)\
**Post date:** [April 5, 2021, 11:30am UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/3 "2021-04-05T11:30:54Z")

</div>

The problem is that right after `imshow()` you need to call `waitKey()` for at least few miliseconds. Within thoes few milieconds OpenCV allows the image to be painted by GUI. Using `time.sleep()` you block the thread for 10 seconds and GUI can not paint the image during that time.

This should solve your problem:

> import cv2  
> import time  
> image = cv2.imread(“./my.jpg”)  
> cv2.imshow(“Image” , image)
> 
> #1 ms for painting  
> cv2.waitKey(1)
> 
> #Block for 10 seconds  
> time.sleep(10)
> 
> #Wait 3 seconds for button press  
> cv2.waitKey(3000)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 5, 2021, 11:41am UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/4 "2021-04-05T11:41:38Z")

</div>

> [@tiglon](#):
>
> and then after 10s , the image came up.

waitKey() does the actual drawing, imshow() just copies an image pointer.

so if you sleep() 10 seconds between imshow() and waitKey(), it won’t draw anything in the meantime

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [April 5, 2021, 11:49am UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/5 "2021-04-05T11:49:34Z")

</div>

> [@FilipBaas](#):
>
> call `waitKey()` for at least few miliseconds

the number doesn’t matter to the “drawing”. every waitKey call “draws” everything that needs to be “drawn”.

the timeout is merely a convenience that keeps the GUI event loop running for that long (or until a key is pressed), in case you don’t need to do anything else during that time.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Silvestri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alejandro_silvestri/32/357_2.png) [@Alejandro\_Silvestri](https://forum.opencv.org/u/Alejandro_Silvestri)\
**Post date:** [April 5, 2021, 1:30pm UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/6 "2021-04-05T13:30:48Z")

</div>

@tiglon

`waitKey` is the function that execute the rendering in HighGUI.

It’s a well documented but no intuitive function. When you use `imshow` in a loop, you should call `waitKey`even if you don’t want to wait for anything.

`waitKey` does three things:

- waits for a keypress
- optionally, waits for a period of time at most, so it can be used as a convenient alternative to sleep()
- renders windows (like pending imshow) before waiting

The waiting is doing by a gui function in GTK, QT or whatever library is used. It is expected to pause the thread with a mutex or equivalent.

---

<div class="post-metadata">

**Author:** ![tiglon](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@tiglon](https://forum.opencv.org/u/tiglon)\
**Post date:** [April 5, 2021, 6:20pm UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/7 "2021-04-05T18:20:36Z")

</div>

@berak @Alejandro_Silvestri thanks for clarifying !!!  
But , is there any way to go into the source code of the waitKey function ???  
As on my local workstation the waitKey code is present in compiled form(binary form ) (have .pyd extension ) and on the github repo of opencv , I am unable to find the code …

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 5, 2021, 6:48pm UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/8 "2021-04-05T18:48:28Z")

</div>

> [@tiglon](#):
>
> is there any way to go into the source code of the waitKey function ???

sure there is, [it’s all on github](https://github.com/opencv/opencv/blob/master/modules/highgui/src/window.cpp#L284)

… just be aware, that opencv is mainly a c++ lib, don’t expect python there !

---

<div class="post-metadata">

**Author:** ![Alejandro\_Silvestri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alejandro_silvestri/32/357_2.png) [@Alejandro\_Silvestri](https://forum.opencv.org/u/Alejandro_Silvestri)\
**Post date:** [April 5, 2021, 7:23pm UTC](https://forum.opencv.org/t/what-actually-waitkey-is-doing/2605/9 "2021-04-05T19:23:35Z")

</div>

If you have GTK, you can see the code [here](https://github.com/opencv/opencv/blob/125b9f60574d40cbd59f5927646c385bba96f526/modules/highgui/src/window_gtk.cpp#L1902).

If you have QT, [this is the code](https://github.com/opencv/opencv/blob/125b9f60574d40cbd59f5927646c385bba96f526/modules/highgui/src/window_QT.cpp#L310).

You can look for `cvWaitKey` in all .cpp files in this [folder](https://github.com/opencv/opencv/tree/master/modules/highgui/src).
