Combination of RawPy and imageio result in red image

611 views Asked by At

I want to do some image processing using .dng files. I am using rawpy to convert the file into a numpy array. Then imageio to save the image. It is a very simple code, given on the rawpy webpage.

The resulting .png image is not as I expect it to be. The result is very red, while the raw image isn't. It is like a red overlay on the original image.

The code I am using is:

import rawpy
import imageio

path = r'img\CGraw.dng'
with rawpy.imread(path) as raw:
    rgb = raw.postprocess()
    
imageio.imsave(r"img\spyraw.png",rgb)

Original dng image: https://drive.google.com/file/d/1Ip4U7KvI4-Lit4u7AuaHfoHIkXvGNRFk/view?usp=sharing

Resulting png image: https://drive.google.com/file/d/1CyrqJS2Osj3u-jEzLSxSy5eKMUP4csAV/view?usp=sharing

1

There are 1 answers

3
Rotem On

Simply add the argument use_camera_wb=True.

According to the documentation:

use_camera_wb (bool) – whether to use the as-shot white balance values

That means using the white balance coefficients that the camera found automatically during the shot (the values are stored as Exif data of the DNG file).

I don't know why the default is use_camera_wb=False. (It could be a result of poor job wrapping LibRaw with Python).
In my opinion the default supposed to be use_camera_wb=True and use_auto_wb=False.

We may also select the output Color Space: output_color=rawpy.ColorSpace.sRGB.

The colors looks right, but there may be other pitfalls that I am not aware of... (I don't have enough experience with rawpy and LibRaw).


import rawpy
import imageio

path = r'img\CGraw.dng'

with rawpy.imread(path) as raw:
    rgb = raw.postprocess(use_camera_wb=True, use_auto_wb=False, output_color=rawpy.ColorSpace.sRGB)
    
imageio.imsave(r"img\spyraw.png",rgb)

Output (downscaled):
enter image description here