pythonrawimage

Python rawpy image saving issues


I am experimenting with merging Astrophotography images in python. Currently I have the issue that when exporting an image with different libraries the image has a different color tone than the original. Imported image is in the .nef format.

The code:

import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy import misc
import rawpy
from PIL import Image
import imageio

raw=rawpy.imread("DSC_5817.NEF")

#raw=rawpy.imread("images/1/slika1.cr2")
rgb=raw.postprocess(no_auto_bright=True)

outfn=".tif"

img=Image.fromarray(rgb)



img.convert("RGB")


img.save("probaPIL"+outfn)
#
cv2.imwrite("progaCV2"+outfn, rgb)

misc.imsave("probaSCipi"+outfn, rgb)
#
imageio.imsave("probaIMAGEIO"+outfn, rgb)

plt.imsave("probaPLT.tif",rgb )

slika=plt.imread("probaPIL.tif")

Screnshot of the output (top middle image is original nef file)


Solution

  • OpenCV (cv2) expects a different channel order (BGR) which is why the colours in that one are off compared to the rest.

    In general there is no single way to postprocess a RAW camera image and the result depends often on personal preferences, e.g. white balance, brightness adjustments etc. What you refer to as "original" in the top middle column is just another postprocessed variant created probably by some Nikon driver which is more aware of certain metadata in the RAW image file and tries to give you something which you "expect". rawpy (or rather, the underlying libraw library) does not replicate the exact behaviour of manufacturer postprocessing, because that's more or less a trade secret.

    For astrophotography you're probably good with a grayscale image anyway, but it really depends on your use case and what you want to achieve.