Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I get the RAW pixel data out of a .NEF file using python?

I'm writing some scripts in python that manipulate raw camera data. At the moment I'm using DCRAW to convert the information from the .nef (Nikon RAW format) file to a .tiff file and then converting it to an sRGB .png in photoshop so that I can use Pillow to read the pixel intensities.

Is there a way to parse the .nef file itself in python, instead of jumping through so many hoops?

I'm not concerned with speed, this is mostly a learning and demonstration exercise.

like image 814
Ryan Avatar asked May 03 '15 04:05

Ryan


1 Answers

You might be able to make use of another library in combination with Pillow to achieve this task. For instance, rawpy could do the job for you.

Their examples at the following link show how to read .nef files and convert them to numpy arrays using the postprocess() method. https://pypi.python.org/pypi/rawpy/0.3.5

From there you can easily save the images as TIFF or maybe do something like.

from PIL import Image
new_img = Image.fromarray(numpy_array)

Edit: I wrote a little script using rawpy and pillow that takes a .nef image, converts it into a numpy array, creates a new pillow image out of it, and shows it on the screen (obviously you can read intensities instead of showing it on the screen)

import rawpy
from PIL import Image
raw = rawpy.imread('trial.nef')
rgb = raw.postprocess()
img = Image.fromarray(rgb) # Pillow image
img.show() # show on screen
like image 175
Gil Avatar answered Oct 16 '22 14:10

Gil