A Nikon RAW (NEF) to EXR converter

Here is a simple converter script nef2exr.py that converts Nikon NEF images to OpenEXR images.

Prerequisites:

import os
import sys
from vop import *
import OpenEXR
import Imath

dc = os.popen("dcraw -c -4 \"%s\"" % sys.argv[1], "r")

dc.readline()
(xs,ys) = [int(x) for x in dc.readline().split()]
dc.readline()

out = OpenEXR.OutputFile(sys.argv[2], OpenEXR.Header(xs, ys))

for y in range(0, ys):
    # Read a line of interleaved RGB 16-bit big-endian samples
    raw = dc.read(xs * 3 * 2)

    # Extract every sixth byte into six arrays
    sixth = [ array(raw[n:len(raw):6]) for n in range(6) ]

    # Reconstruct seperate R,G,B channels
    red = sixth[0] * 256 + sixth[1]
    grn = sixth[2] * 256 + sixth[3]
    blu = sixth[4] * 256 + sixth[5]

    # Write out the line to the EXR file
    out.writePixels({'R': red.toraw(), 'G' : grn.toraw(), 'B' : blu.toraw()}, 1)
out.close()

It takes about 20 seconds to convert a 3Kx2K image.