letmaik / rawpy

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

How to extract bayer pattern #179

Closed thomaskleiven closed 1 year ago

thomaskleiven commented 1 year ago

Is it possible to split the raw data up into its red, green, and blue components before demosaicing?

It seems like that raw data is available using raw_image (docs)

file_path = 'sample1.dng'
with rawpy.imread(file_path) as raw:
    img = raw.raw_image   

    print(img[0,0]) ## Prints 158

Provided that the sensor data is on the format RGGB, then img[0,0] would correspond to the first red pixel of the sensor data.


Goal: Extract the value of the first red pixel

Method 1) Using rawpy, the value of img[0,0] is 158.

Method 2) Using RawTherapee (docs) to read the same image, it suggests that the first red pixel (before demosaicing) is 57 as seen below.

rawtherapee_sample

Example RAW image: sample1.dng found (here: google drive)

I'm probably missing some basic logic here, but is it possible to obtain the same value using rawpy? Thanks in advance for any pointers.

kmilos commented 1 year ago

rawpy (LibRaw that is) and RawTherapee might be providing you with different crops of the raw array in the file, so "first pixel" is a relative term here. Does raw_image_visible perhaps correspond better?

Run e.g. exiftool -a -u -s -g1 on your DNG file and see the many tags related to image size, active area, and crops. Different libraries might interpret the crops differently.

Another thing: LibRaw/rawpy should show you the real raw pixel value in raw_image/raw_image_visible. Don't know what RT shows in that screenshot? Does it include black level or not (seems it is already removed in the histogram)? Any stretching/scaling to white point? "57" out of what, 255 or 65535?

In any case, I don't see any issues here w/ rawpy. Perhaps you might want to close this and move it over to discussions.

thomaskleiven commented 1 year ago

Thanks for the pointers. Didn't manage to get exactly the same histograms pr channel using rawpy (LibRaw) vs. RT, but as you mention, this is probably not an issue with rawpy as RT might do some additional cropping, etc. Happy to move the issue to discussions.