#!/usr/bin/perl ### Process Canon RAW files ### # (C) 2006, Eli Glanz ### # This script will create a preview JPG and a full-size TIF for a given CR2 file. # The output files will have the same headers and timestamp as the input file. ### # Command line arguments: # process-raw-images.pl [-help] [-nopreview] [-notif] [-nooverwrite] [-todir ] # -nopreview don't generate the preview image\n"; # -notif don't generate the TIF output image\n"; # -nooverwrite don't overwrite existing files\n"; # -todir the directory to which to save output files\n"; # file a RAW image to process\n"; # directory process all RAW images in this directory\n"; use strict; use Cwd; # the location of exiftool (http://www.sno.phy.queensu.ca/~phil/exiftool/) my $EXIFTOOL = "/usr/bin/exiftool"; # the location of dcraw (http://www.cybercom.net/~dcoffin/dcraw/) my $DCRAW = "/usr/bin/dcraw"; # the location of ppm2tiff (http://www.libtiff.org/man/ppm2tiff.1.html) my $PPM2TIFF = "/usr/bin/ppm2tiff"; # extract the preview image from the RAW file my $CREATE_PREVIEW_CMD = "$EXIFTOOL -b -PreviewImage INPUTFILE > DIR/TITLE-preview.jpg"; # copy EXIF header from the original file to the preview file my $COPY_HEADERS_CMD = "$EXIFTOOL -overwrite_original_in_place -TagsFromFile INPUTFILE -all:all DIR/TITLE-preview.jpg"; # Step 1 of creating a TIF: convert RAW file to PPM my $CREATE_PPM_CMD = "$DCRAW -wz DIR/INPUTFILE"; # Step 2 of creating a TIF: convert PPM to TIF my $CREATE_TIF_CMD = "$PPM2TIFF DIR/TITLE.ppm DIR/TITLE.tif"; my $createPreview = 1; my $createTIF = 1; my $overwriteFiles = 1; my $imageCount = 0; sub processDirectory { my $dir = shift; my $outputdir = shift; print "Processing contents of $dir\n"; opendir CURDIR, $dir or die "Unable to get listing of $dir: $!"; my @files = readdir CURDIR; foreach my $file (@files) { # we only care about CR2 (Canon Raw) files next if ($file !~ /CR2/i); processFile ($dir."/".$file, $outputdir); } closedir CURDIR; } sub processFile { my $file = shift; my $outputDir = shift; my ($title, $extension) = $file =~ m/(.*?)\.(\w\w\w)/; my ($atime, $mtime) = (stat($file))[8,9]; print "Processing file $file"; # for extra safety, make the original image file read-only chmod 0440, $file; if ($createPreview == 1) { if ($overwriteFiles == 1 || ! (-e "$title-preview.jpg")) { # create the preview image my $cmd = buildCmdLine ($CREATE_PREVIEW_CMD, $file, $outputDir); print "."; `$cmd`; # then update the preview file's headers my $cmd = buildCmdLine ($COPY_HEADERS_CMD, $file, $outputDir); print "."; `$cmd`; # update the timestamp's of the new file to match the original file's utime $atime, $mtime, "$title-preview.jpg"; } } if ($createTIF == 1) { if ($overwriteFiles == 1 || ! (-e "$title.tif")) { # now create the PPM my $cmd = buildCmdLine ($CREATE_PPM_CMD, $file, $outputDir); print "."; `$cmd`; # now create the TIF my $cmd = buildCmdLine ($CREATE_TIF_CMD, $file, $outputDir); print "."; `$cmd`; # and update the TIF's headers my $cmd = buildCmdLine ($COPY_HEADERS_CMD, $file, $outputDir); print "."; `$cmd`; # update the timestamp's of the new file to match the original file's utime $atime, $mtime, "$title.tif"; # delete the PPM file unlink "$title.ppm"; } } print "\n"; $imageCount++; } sub buildCmdLine { my $cmd = shift; my $inputFile = shift; my $outputDir = shift; my ($title, $extension) = $inputFile =~ m/(\w*?)\.(\w\w\w)/; $cmd =~ s/INPUTFILE/$inputFile/g; $cmd =~ s/TITLE/$title/g; $cmd =~ s/DIR/$outputDir/g; return $cmd; } sub normalizeDirectory { my $dir = shift; return Cwd::abs_path($dir); } sub printHelp { print "Process RAW image files\n\n"; print "\t".$0." [-help] [-nopreview] [-notif] [-nooverwrite] [-todir ] \n\n"; print "\t-nopreview\tdon't generate the preview image\n"; print "\t-notif\t\tdon't generate the TIF output image\n"; print "\t-nooverwrite\tdon't overwrite existing files\n"; print "\t-todir\t\tthe directory to which to save output files\n"; print "\tfile\t\ta RAW image to process\n"; print "\tdirectory\tprocess all RAW images in this directory\n"; } my $arg = shift; my @files; my $outputdir = "."; while (defined($arg)) { if ("-notif" eq $arg) { $createTIF = 0; } elsif ("-nopreview" eq $arg) { $createPreview = 0; } elsif ("-nooverwrite" eq $arg) { $overwriteFiles = 0; } elsif ("-todir" eq $arg) { $outputdir = normalizeDirectory(shift); } elsif ("-help" eq $arg) { printHelp; exit 1; } else { push @files,$arg; } $arg = shift; } if ($#files == -1) { printHelp; exit 1; } if ($createPreview == 1) { die "Can't find exiftool\n" if (! -e $EXIFTOOL); } if ($createTIF == 1) { die "Can't find dcraw\n" if (! -e $DCRAW); die "Can't find ppm2tiff\n" if (! -e $PPM2TIFF); } die "$outputdir is a file.\n" if (-f $outputdir); mkdir($outputdir) if (! -e $outputdir); foreach $arg (@files) { if (! -e $arg) { print "Can't find file: ".$arg."\n"; next; } if (! -r $arg) { print "Can't read input file ".$arg."\n"; next; } if (-d $arg) { processDirectory normalizeDirectory($arg), $outputdir; } else { if ($arg !~ /CR2/i) { print "$arg is not a CR2 file.\n"; next; } processFile $arg, $outputdir; } } print $imageCount." images processed.\n";