Created
February 13, 2011 16:08
-
-
Save mschmitt/824808 to your computer and use it in GitHub Desktop.
Script that imports photos and names them according to EXIF timestamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use diagnostics; | |
use File::Basename; | |
use File::Copy; | |
use File::Find; | |
use Data::Dumper; | |
use Image::ExifTool; | |
use Digest::MD5; | |
my $target_dir = "$ENV{'HOME'}/incoming-photos"; | |
unless (-d $target_dir){ | |
print "No such directory: $target_dir\n"; | |
exit 1; | |
} | |
sub wanted{ | |
my $file = $_; | |
my $filename = basename($file); | |
unless ((-f $file) and ($file =~ /\.(CR2|JPG|DNG|CRW)$/i)){ | |
# print "$file: No photo. Skip.\n"; | |
return; | |
} | |
my $exifTool = new Image::ExifTool; | |
$exifTool->Options(DateFormat => '%Y%m%d_%H%M%S'); | |
my $info = $exifTool->ImageInfo($file); | |
my $datetime = $info->{'DateTimeOriginal'}; | |
my $target = sprintf("%s/%s_%s", $target_dir, $datetime, $filename); | |
copy($file, $target); | |
open my $src_file, "<$file" or die "Can't read $file: $!\n"; | |
open my $tgt_file, "<$target" or die "Can't read $target: $!\n"; | |
my $src_dgst = Digest::MD5->new; | |
my $tgt_dgst = Digest::MD5->new; | |
$src_dgst->addfile($src_file); | |
$tgt_dgst->addfile($tgt_file); | |
my $src_md5 = $src_dgst->hexdigest; | |
my $tgt_md5 = $tgt_dgst->hexdigest; | |
close $src_file; | |
close $tgt_file; | |
if ($src_md5 eq $tgt_md5){ | |
print "Copied: $file -> $target\n"; | |
}else{ | |
print "!!! Checksum mismatch, File: $file\n"; | |
} | |
} | |
find ({wanted => \&wanted, no_chdir => 1}, "."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment