Sorting images into Exif Date Taken folder

So tonight I decided i’d had enough dealing with backups and storage space contraints on my home PC so I figured Google Drive @ USD$4.99 p/month for 100GB, is a bargin for a little less stress in my life. This of course left me wondering what to actually upload.

I picked my photos to start, but was immediately faced with a problem. Well two actually:

  1. A large amount of images all share similar names across various folders
  2. Not all folders have photos sorted by date or by their actual Exif “Date taken” timestamp.
    Most of the photos are all sorted in to the yyy-mm-dd of the day they were extracted from the camera to the PC and over the years, some where just organized where-ever.

Solution?

Script something to fix it!

first thing to do was to rename the files uniquely and then move them into their respective yyyy-mm-dd folder based on the images exif date-taken timestamp.

 
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use autodie;
 
use Path::Class;
use File::Copy;
use Digest::MD5 'md5_hex';
use Image::ExifTool 'ImageInfo';
 
sub rename_by_exif_DateTaken {
# and make the filenames unique
     for my $f ( dir()->children ) {
          next if $f->is_dir;
          my $exif = Image::ExifTool->new;
          $exif->ExtractInfo($f->stringify);
          my $date = $exif->GetValue('DateTimeOriginal','PrintConv');
 
          next unless defined $date;
          $date =~ tr[ :][T-];
 
          my $digest = md5_hex($f->slurp);
          $digest = substr($digest,0,7);
          my $new_name = "$date-$digest.jpg";
 
          unless ( $f->basename eq $new_name ) {
               rename $f => $new_name;
          }
     }
}
 
rename_by_exif_DateTaken ;
 
sub sort_into_date_folders {
# yyyy-mm-dd
  for my $f ( dir()->children ) {
	next if $f->is_dir;
 
	my $exif = Image::ExifTool->new;
	   $exif->ExtractInfo($f->stringify);
 
	my $timestamp = $exif->GetValue('DateTimeOriginal', 'PrintConv');
		next unless defined $timestamp;
		$timestamp =~ tr[ :][T-];
	my ($date, undef) = split /T/, $timestamp;
 
	print "$date\n";
 
	if ( ! -d $date ) {
		mkdir($date);
	}
 
	print "moving $f -> $date/$f\n";
	move("$f", "$date/$f");
 
  }
}
 
sort_into_date_folders ;

kudos to David Golden for the original inspiration.

Date
Categories
Tags
Permalink
Status

Published:May 17, 2013

Scripting

Bookmark the permalink

Both comments and trackbacks are currently closed.