picamera2 icon indicating copy to clipboard operation
picamera2 copied to clipboard

JPEF or DNG images as buffers

Open dgalland opened this issue 2 years ago • 13 comments

It may be useful in certain applications to obtain the jpeg or dng image in a buffer rather than writing it to a file. This is not currently possible with the Request or Helpers methods. For example PiDNG convert can return the buffer if filename == "" but not the helper.

dgalland avatar Jan 08 '24 09:01 dgalland

Hi, does it not work to pass a BytesIO as the "file", a bit like this example? If it doesn't that would probably be a bug as I'm sure it's the intention!

davidplowman avatar Jan 08 '24 09:01 davidplowman

Yes it probably works for Picamera2 methods but not for Request methods which ask for a fileName. Something like this: while .....
request = picam2.capture_request() array = request.make_array("raw") or "main" convert in a jpeg or dng buffer

dgalland avatar Jan 08 '24 11:01 dgalland

I'm sorry, I still don't think I've understood what you want. So far as I know, this type of thing should work:

...
request = picam2.capture_request()
data = io.BytesIO()
request.save('main', data, format='jpeg')

Can you be more explicit about what it is that you can't do? Thanks!

davidplowman avatar Jan 08 '24 11:01 davidplowman

Yes, this should work for a JPG or PNG but not for a DNG ?

dgalland avatar Jan 08 '24 12:01 dgalland

PiDNG is a third party library (which we're very happy to be able to use, of course), so we would have to talk to the developer in question. I don't expect it would be difficult to fix, but obviously someone has to have the time to get round to it. It might be worth posting on the PiDNG repo, though I don't know if the author is able to monitor it very closely.

As a workaround one can always write to a memory file (in "/dev/shm"). It is a bit cumbersome, but probably gets you many of the benefits.

davidplowman avatar Jan 08 '24 12:01 davidplowman

Yes but PIDNG is already able to return the buffer if filename=="" But this possibility disappears in the Helper.

This is just a comment because there are workarounds like using shm or calling directly PiDNG by copying the Helper code This is all for experiments. PiDNG is an interesting tool but it should still be noted that it is extremely slow, 100% Python and therefore not possibly multithreaded.

dgalland avatar Jan 08 '24 12:01 dgalland

Ah, I see. Well, I suppose I might arguably be happier to see PiDNG handle it because modules like PIL, cv2 do, though I'm not particularly averse to putting a wee bodge into save_dng to detect a BytesIO and work round it.

davidplowman avatar Jan 08 '24 13:01 davidplowman

Just to double-check, if I'm going to put in a workaround for this, the test case for the bug report is this:

from picamera2 import Picamera2
import io

picam2 = Picamera2()
data = io.BytesIO()
picam2.start()

picam2.capture_file(data, 'raw')
print(data.getbuffer().nbytes)  # prints 0, and it shouldn't

But I believe JPEGs are OK as things stand.

davidplowman avatar Jan 12 '24 10:01 davidplowman

Hi David, Yes but we would also have to change the Request method def save_dng(self, filename, name="raw"): and also the Helpers ? def save_dng(self, buffer, metadata, config, filename): The most consistent is perhaps to call PiDng with filename="" ??

dgalland avatar Jan 13 '24 17:01 dgalland

Maybe have a look whether https://github.com/raspberrypi/picamera2/pull/923 fits the bill?

davidplowman avatar Jan 15 '24 10:01 davidplowman

Yes that looks correct to me, thank you !

dgalland avatar Jan 15 '24 17:01 dgalland

Great! - when will this show up in the official distribution?

cpixip avatar Jan 16 '24 17:01 cpixip

I'll see about getting another update out in the next week or so. In the meantime, you can always clone the repo or pip install directly from git to get early access.

davidplowman avatar Jan 17 '24 08:01 davidplowman