Image got damaged when sending through USB-VCP

when i transfer an image through the USB-VCP,a part of it got corrupted sometimes.here is an example:

Here is the code i used

while(True):
    if usb.any():
        cmd = usb.recv(4, timeout=5000)
        if (cmd == b'snap'):
            img = sensor.snapshot().compress()
            usb.send(ustruct.pack("<L", img.size()))
            usb.send(img)

What cause this problem and how to solve it?
thanks in advance.

maybe some info lost due to synchronization with controller?
what controller you use to read the image?

I would like to try that.
did you tried rpc library? There are examples on the ide that works fine for continuous image transfer.
they work with these controllers:

what I want is to save a single image on a single event and maybe your approach may work for me cause i don’t mind for this kind of loss in pixels.

Hi, welcome to serial comms. Life is hard and not easy.

We do not provide support for figuring out comms as it’s not an error in our software. You have to have robust code that handles all kinds of nonsense to make data transfer work.

i use pc to read the image