I am trying to read a Nikon RAW (NEF) image correctly, and have found there have been a few suggestions; rawpy, imageio, and simple numpy reads:
RAW Image processing in Python
However, I'm getting some odd results when reading in Nikon RAW images (so far, imageio does not work with Sony ARW, another format I'd like to read).
Code is here:
# -*- coding: utf-8 -*-
"""
Code to import RAW image from Nikon NEF file, in two ways.
Display with opencv and matplotlib
"""
import numpy as np
import cv2
import rawpy
import matplotlib
import matplotlib.pyplot as plt
import imageio
# Reading a Nikon RAW (NEF) image
# This uses rawpy library
filename='DSC_0001.NEF'
print("reading RAW file using rawpy.")
raw = rawpy.imread(filename)
image_rawpy = raw.postprocess()
print("Size of image read:" + str(image_rawpy.shape))
# Optional
# Convert RGB to BGR
# image_rawpy = image_rawpy[:, :, ::-1].copy()
####################
# Show using matplotlib
fig = plt.figure("image_rawpy read file: " + filename)
plt_image = image_rawpy
imgplot = plt.imshow(plt_image)
plt.show(block=False)
# Show using OpenCV
cv2.imshow("image_rawpy read file: " + filename, image_rawpy)
####################
# This uses imageio
print("reading RAW file using rawio.")
image_imageio=imageio.imread(filename)
print("Size of image read:" + str(image_imageio.shape))
fig2 = plt.figure("image_imageio read file: " + filename)
plt_image2 = image_imageio
imgplot2 = plt.imshow(plt_image2)
plt.show(block=False)
# Show using OpenCV
cv2.imshow("image_imageio read file: " + filename, image_imageio)
cv2.waitKey()
cv2.destroyAllWindows()
When reading with rawpy, it reads the full size of the array (4000x6000), but with imageio, it only seems to read a thumbnail (120x160). The rawpy method shows an image with a bit of a pink hue (see note below), and the imageio one is a simple grayscale image.
Thankfully, these results are the same on both an Ubuntu and Win10 box.
[Note - the Nikon camera is set up for infrared photography with the removal and addition of filters internally, which we can't access. This will affect what hits the sensor, but the resulting image should be viewed the same from the software side.]
The question
Any better ways of reading Nikon RAW images in Python? Has a canonical reference been missed?