Load JPEG image from buffer

How can we load a jpeg image buffer to an image object? I found examples with jpeg file only.

Once this PR is merged you will be able to just pass a path: imlib: Enable loading images from paths in all situations. by kwagyeman · Pull Request #1962 · openmv/openmv · GitHub

Until then:

img = Image.Image(“jpg_file_path”, copy_to_fb=True)

Then you can use the image object. Note that when you do this the system will turn off excess frame buffers so you’ll notice the FPS drop if you decide to use the sensor.snapshot() method again. To fix, reset the resolution, pixformat, or etc.

Hi @kwagyeman

I do not have a file but a buffer with jpeg data in memory.

jpg_buf = b’…’
img = Image.Image(jpg_buf, copy_to_fb=True)

Will this work?

Oh, I see.

Hmm, there’s no support right now for that. However, if you write it to a file with .jpg at the end of the name then you can load it.

I guess the API should be extended to allow casting bytearrays to images.

The jpeg buffers (650+) are coming from a stream and making a file every time would be costly.

Yes, please!

I wanted to render jpeg buffer to an LCD. Is there any other way to achieve this in OpenMV?

When it’s merged you can then use the IDE to just install the latest development release.

Note, this does a deepcopy of the input array. I could have done a shallow copy… but, then it would have made the function more complex. That said, hopefully you have enough memory where this is not an issue.

1 Like

I think it has been merged so I downloaded and flashed the latest development release but it seems not working.

import image 

buf = None 

image.Image(80, 40, image.RGB565, buf)

TypeError: extra positional arguments given

image.Image(80, 40, image.RGB565, buffer=buf)

1 Like

Thanks, I missed that it is a keyword argument.

I’ve come across a similar situation when working with JPEG images and buffers. I found examples for loading from a JPEG file, but dealing with image buffers was a bit trickier. In my experience, merging and using the IDE to install the latest development release helped a lot. Also, I get the memory concern – a deepcopy might be safer. By the way, have you looked into how to compress JPEG images for more efficiency?