letmaik / rawpy

📷 RAW image processing for Python, a wrapper for libraw
https://pypi.python.org/pypi/rawpy
MIT License
591 stars 67 forks source link

Feature Request: Load raw image from numpy array #83

Closed JustinMBrown closed 5 years ago

JustinMBrown commented 5 years ago

RawPy can't parse my .raw image, but I can do it using numpy. It would be nice to be able to create a RawPy image object from a numpy array, rather than a path to a file

letmaik commented 5 years ago

What are you trying to solve? What format is your .raw file in? How do you read it in numpy?

JustinMBrown commented 5 years ago

The .raw is a sensor dump. I'm reading it using np.fromfile(path, dtype='>u2'). I then reshape it to the correct image size. It's a single channel image that hasn't been debayered. I want to be able to do something like img = rawpy.fromarray(np_array). That way I can start using rawpy methods on img.

letmaik commented 5 years ago

rawpy is built on top of libraw which reads RAW camera file formats and then offers the option to access the raw (Bayer) image and also to postprocess/debayer it. Your file is not a standard camera raw file format, hence it doesn't work. Currently, libraw doesn't allow to load raw sensor dumps. Typically, postprocessing makes use of metadata stored in the raw camera file, to handle things like brightness adjustment, color curves, white balance, knowing where the visible image area is etc. I guess libraw would have to be extended to supply these information separately if it was accepting sensor dumps directly, but I doubt that's a good idea in general.

If you want to use rawpy then I think your best bet would be to figure out a way to dump/convert your sensor dump into DNG format, which is a vendor-neutral RAW file format from Adobe and widely accepted, also by rawpy.

If going for DNG is too much effort, then possibly an alternative is to debayer yourself, in the end it's just interpolation. I just found https://colour-demosaicing.readthedocs.io/en/develop/index.html which looks like it fits your case perfectly.

JustinMBrown commented 5 years ago

Thank you for the help. I ended up using OpenCV to debayer them.

And this is off topic, but it seems weird to me that sensor dumps aren't the default/standard. I get that file formats make processing easier, but I would expect a package called LibRaw to be able to handle sensor dumps, or at least pre-formatted arrays like I have. Anyways, thanks again.

letmaik commented 5 years ago

For reference, can you share how you used OpenCV to debayer?

JustinMBrown commented 5 years ago

https://stackoverflow.com/questions/22308581/python-opencv-debayer

Relevant bit:

img = cv2.cvtColor(img, cv2.COLOR_BAYER_BG2BGR)

HongChow commented 2 years ago

RawPy can't parse my .raw image, but I can do it using numpy. It would be nice to be able to create a RawPy image object from a numpy array, rather than a path to a file

cannot agree more