# CV2.imwrite causing latency in program

**URL:** <https://forum.opencv.org/t/cv2-imwrite-causing-latency-in-program/18102>\
**Category:** Python\
**Tags:** videoio, imgcodecs\
**Created:** [June 27, 2024, 3:32pm UTC](https://forum.opencv.org/t/cv2-imwrite-causing-latency-in-program/18102 "2024-06-27T15:32:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bh4](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bh4/32/10232_2.png) [@bh4](https://forum.opencv.org/u/bh4)\
**Post date:** [June 27, 2024, 3:32pm UTC](https://forum.opencv.org/t/cv2-imwrite-causing-latency-in-program/18102/1 "2024-06-27T15:32:52Z")

</div>

All,

I have a python code using cv2.imwrite to record images from a capture card (or videos, but I think that opencv can only record as frames from what I know). However, the function is causing a buildup latency in all the data displayed to my GUI (I have some other data being transferred and displayed from a local C++ server). That problem is gone or can be neglect after I command out cv2.imwrite.

Since I do need to save the image for recording purposes, I was wondering if there is a way I can get around of the latency issue caused by cv2.imwrite

---

<div class="post-metadata">

**Author:** ![bobatproventus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bobatproventus/32/10807_2.png) [@bobatproventus](https://forum.opencv.org/u/bobatproventus)\
**Post date:** [June 28, 2024, 7:39pm UTC](https://forum.opencv.org/t/cv2-imwrite-causing-latency-in-program/18102/2 "2024-06-28T19:39:43Z")

</div>

Hello, are you able to use threads?

So you can offload the image saving to a different thread instead of waiting for it to finish

Something like this (this is like a pseudocode):

```auto
import cv2
import threading
import queue

image_queue = queue.Queue()

# then you can put on the queue on each capture
filename = 'test_filename.png'
image_queue.put((frame, filename))
# And show it later
cv2.imshow('frame', frame)

# then take them on another thread to save them:
def save_images():
    while True:
        image, filename = image_queue.get()
        if image is None:
            break
        # Save the image
        cv2.imwrite(filename, image)
        image_queue.task_done()
threading.Thread(target=save_images, daemon=True).start()

```

---

<div class="post-metadata">

**Author:** ![bh4](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bh4/32/10232_2.png) [@bh4](https://forum.opencv.org/u/bh4)\
**Post date:** [July 5, 2024, 8:54pm UTC](https://forum.opencv.org/t/cv2-imwrite-causing-latency-in-program/18102/3 "2024-07-05T20:54:58Z")

</div>

Thank you. I have converted my code to a multi thread code to avoid the slow down caused by cv2.imwrite.
