Working with Nikon RAW (.nef) photo’s on Linux

If you own a digital SLR (Single Lens Reflex) camera, there comes a time when you want to move to RAW format because it gives you so much more options of processing the photo afterwards. Also the price of memory cards has decreased tremendously, so the extra storage space needed is no reason anymore to keep shooting in JPEG. On my last holiday to Canada i took 3 compact flash cards of 4GB each with me, so i had room for around 2000 photo’s with my Nikon D70S.

When i returned from this fantastic holiday and mounted the memory cards on my Linux box to look at the results, i had a bit of a disappointment because the .nef files were not recognized by gqview, my favorite photo viewing program. Also i couldn’t put them straight away on the website, because nobody would see the picture when clicking on the nef images.

After a little bit of searching on Google i discovered that a Nikon NEF image contains four JPG images, so you don’t need to convert the NEF to JPG, but you can just extract the desired jpg image from the NEF file. Phil Harvey has written the excellent exiftool to do that (and a lot more) on Linux. The syntax to do this is:

exiftool -b -JpgFromRaw orignal.NEF > output.jpg

If you want to do this for a lot of files, you can use the command like this:

exiftool -b -JpgFromRaw -w _C2007.JPG -ext nef -r .

The command specifies to operate recursively on all files in the current directory (-r .) and use the original filename with _C2007.JPG appended as output filename.
But it turned out that the EXIF information which is present in the original NEF image is not copied to the resulting JPG image. Luckily you can do that with exiftool in a second step:


exiftool -b -JpgFromRaw original.NEF > output.jpg

Before putting these images on my website i wanted to scale them down to a viewable dimension and also rename the image to a more sensible name. By choosing a name based on the date i took the photograph, the images would automatically be listed (and viewed) in chronological order. Also because i took two camera’s with me on holiday, this would allow me to merge both photo sets into one directory and still have them in chronological order.

To automate all the steps, i wrote a small perl program which i named neftojpg.pl

[perl]
#!/usr/bin/perl -w
#
# wil create a set of JPEG images from NIKON NEF files
# resize those to $pix pixels
# copy the EXIF data over from NEF to JPEG
# and rename the JPEG files to a filename based on the
# creation date

# 20071205 (c) Ewald

use strict;

my @files;
my $file;
my $name;
my $pix=”800×600″; # desired geometry
my $dir=”.”;
my $dirname=”small”; # output subdirectory
my $ext=”nef”; # nef for NIKON, crw for Canon

@files = <$dir/*.$ext>; # set of files to work on

foreach $file (@files) {
print “processing $file\n”;

($name) = ($file =~ /.*\/(.+?)\.$ext/); # separate name from file extension

if (!-d $dirname) {
mkdir $dirname or die “$!”;
}

# extract jpg image from raw and convert to $pix pixels
qx{exiftool -b -JpgFromRaw $file | convert – -geometry $pix $dirname/$name.jpg};

# copy exif tags from the raw image into the jpg file
qx{exiftool -TagsFromFile $file $dirname/$name.jpg};

# rotate image if nessessary based on EXIF information (NB your camera might not support this)
qx{exiftran -ai $dirname/$name.jpg};

# rename jpg file to a date based name
qx{exiftool ‘-FileName$ext to CRW the script should just as easy work for Canon RAW image files.

Both exiftool and ImageMagick (where the convert command comes from) have their own perl modules, so the next step would be to get rid of the qx{} to execute shell commands and rewrite all operations in perl, but i am in no hurry yet to do so 😉