Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Created February 13, 2011 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschmitt/824808 to your computer and use it in GitHub Desktop.
Save mschmitt/824808 to your computer and use it in GitHub Desktop.
Script that imports photos and names them according to EXIF timestamp
#!/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