0

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?

asylumax
  • 781
  • 1
  • 8
  • 34
  • 1
    The OpenCV link to question by `mgoubert` (the second link in your question) is not relevant as it refers to a `raw` file by which OP means a raw binary file with no header, no width, no height, no date - just pure pixel data. That is fundamentally different from what a photographer or camera manufacturer means by a raw file which is one that has not been de-bayered, or calibrated to any known, conventional colourspace. – Mark Setchell Mar 31 '20 at 13:07
  • Updated removed less relevant link and duplicate. – asylumax Mar 31 '20 at 13:21
  • 2
    I don't know what anyone would really call the definitive reference for how a camera manufacturer's raw should be interpreted. Personally, I would tend to lean towards Adobe's *"DNG Converter"* as being about as close as you'll get to a reference... https://helpx.adobe.com/uk/photoshop/using/adobe-dng-converter.html – Mark Setchell Mar 31 '20 at 13:58

0 Answers0