Sending images over USB

Hi, I know this is a simple question but I’m having difficulty finding a direct answer. Are there any standard methods of sending images over USB? I am going to be communicating with a custom program. I currently am planning on just iterating through all the pixels of an image with image.get_pixel(), but figured I’d ask if there was maybe a better way first.

Just do img.compressed() to create a jpeg compressed image. Then just send the length of the image and then the image. The compressed() method returns a jpeg compressed image which can be accessed like a byte array and send like any bytes() object.

Alternatively, you can use a format for the IDE by doing… img.compressed_for_ide() and then sending the resulting bytes() object.

Here’s how to decode the compressed_for_ide() datastream:

Basically, when you see a byte with the top two bits equal to “1” and “0” append those bytes to a queue. Then, when you get an 0xFE byte you decode all the bytes in the queue like this:

And you’ll get a jpeg bytestream after decoding which you can turn into a jpeg image.

Using the IDE format is recommended over channels that may drop bytes as the format is designed to handle missing bytes gracefully. If you’re going over USB it offers no benefit since bytes won’t be dropped.