Joe's Linux Blog Linux Admin tips and tricks

August 24, 2009

Copying Nikon RAW pictures to JPEG

Filed under: Tools — Tags: , , , , , — jfreivald @ 2:19 pm

My wife loves her Nikon camera.  She also loves Photoshop.  The two go together really well.  She takes pictures in the raw “NEF” format, and photoshop works miracles on them.  Unfortunately, for sharing snapshots it’s always a pain in the butt to get each and every picture that we want to share converted to JPEG so that everyone who doesn’t have photoshop can use them.  Not to mention that even as JPEGs, a 10 megapixel photo is too big to go e-mailing to Aunt Laura on her Dial-Up.

The dichotomy is clear: Quality vs. Portability.

So like everything else that takes for ever and is tedious, I wrote a script.  This one looks in the underlying tree and checks to see if each NEF file has a corresponding JPEG file.  If it doesn’t, then it creates one using ImageMagick.  If there is one, it ignores the file and moves on to the next one. Now she can have the super-high quality RAW pictures, and I can e-mail them to grandma.  Once again, everyone is happy in Joeland.

In this case we also convert the size of the image to two megapixels, which is plenty for sharing photos, but not great for printing blow-ups.  But that’s okay, because we still have the original NEF to manipulate if we want to!

On Centos I had to do a ‘cpan install autodie’ and ‘cpan install IPC::System::Simple’ to get this to compile right.  Autodie is nice because if you hit Ctrl-C to stop the script then it will actually stop instead of continuing on to the next picture.

Here is the script:

#!/usr/bin/perl

use autodie qw(:all);

@FILES = split(/\n/, `find . | grep "\.NEF\$"`);
foreach $file(@FILES) {
    $rawfile = $file;
    $file =~ s/NEF$/small.JPG/;
    if (-d $file) {
        print "Entering directory $file.\n";
    } elsif (! -f $file) {
        print "\t$rawfile -> $file\n";
        system("convert \"$rawfile\" -normalize -resize \"\@2000000\" \"$file\"");
    } else {
        print "\t$rawfile skipped.  $file already exists.\n";
    }
}

Enjoy!

--JATF

Powered by WordPress