Reading NIKON Raw files in Python

I wanted to read a Nikon raw file using Python. On the off chance that someone else wants to do it, I thought i'd write it down. Actually, it turns out that other people have done that. I found this awesome nef_decoder. Once you have downloaded all the files in that directory (I used SiteSucker to do that). You have to install the python module (see this post for details). If you're lucky, a simple python install will work. Because of cython compatibility issues, it didn't quite work. I got the following error message:

pixelutils.pyx:207:13: 'bool' is not a type identifier

Luckily for me, somebody had this problem before and I could easily solve it by adding a line at the top of pixelutils.pyc:

from cpython cimport bool

I added the line and restarted the install and it finished installing without any problem. I then opened IPython and entered the following code:

import nef_decoder
file = "myfile.NEF"
md, mn, im = nef_decoder.decode_file(file)

That should have worked but it didn't. I got a KeyError on line
--> 295 info['img_orientation'] = ifd[img_orientation_tag_id][-1]
Since I was only interested in the image and not the EXIF tag about the image orientation. I have simply bypassed the problem (not pretty, I know) with Try and Except:

   info['img_orientation'] = ifd[img_orientation_tag_id][-1]
   info['img_orientation'] = 0

I tried again (reloaded the module and restarted decode_file) and it worked. I had a nice RGB image to work with. Thanks all for solving my problems today.


  1. I tried this, but didn't work. Do you have some idea why?

    \", line 459, in decode_pixel_data
    curve_max_len = 1 << tiff_bps & 0x7fff
    NameError: global name 'tiff_bps' is not defined


Post a Comment

Popular Posts