Welcome!
Friday 03 September 2010 @ 19:08 CEST

Sort pictures based on Exif date

TechRecently, I bumped into a problem: I had received pictures from same event from three different sources (cameras). How could I sort these based on when the pictures were taken? A small Perl script are born to the rescue. The program "jhead" dumps Exif data contained in JPEG images. Running jhead on a image without options gives:

$ jhead IMG_1195.JPG
File name : IMG_1195.JPG
File size : 2988387 bytes
File date : 2007:03:04 15:37:34
Camera make : Canon
Camera model : Canon DIGITAL IXUS 800 IS
Date/Time : 2007:02:22 19:17:14
Resolution : 2816 x 2112
Flash used : Yes (auto)
Focal length : 5.8mm (35mm equivalent: 37mm)
CCD width : 5.72mm
Exposure time: 0.017 s (1/60)
Aperture : f/2.8
Whitebalance : Auto
Metering Mode: matrix

The "Date/Time" header gives the time stamp. Renaming the file to the date, would make it easy to sort the pictures in time based on filename:

IMG_1195.JPG --> 20070222_191714.jpg

Instead of manually renaming a couple of hundred images, Perl can do the job:

#!/usr/bin/perl -w
#
# filename: exif_date_sort.pl
#
# Small utility that renames pictures based on the Exif date.
#
# Date: Sun Mar 4 19:13:42 CET 2007
#
# Lars Strand
#
use strict;
use File::Glob qw(:globally :nocase);
use File::Copy;

die "Usage: $0 PATH" if $#ARGV < 0;

while (@ARGV) {
my @filelist = glob($ARGV[0]."*.jpg");
foreach my $file (@filelist) {
for my $line (`jhead "$file"`) {
if ($line =~ /^Date\/Time\s*:\s*(\d+):(\d+):(\d+)\s*(\d+):(\d+):(\d+)/) {
print "$file --> $1$2$3_$4$5$6.jpg\n";
copy("$file", "$1$2$3_$4$5$6.jpg") or die "Error: Copy failed: $!";
}
}
}
shift @ARGV;
}

The script takes one or more directories as argument. Every JPG file that has a Exif date header, are copied to a new "date-filename".

Running it gives the following output:

$ ~/exif_date_sort.pl 200702-Veggli/
200702-Veggli/IMG_1196.JPG --> 20070222_194000.jpg
200702-Veggli/IMG_1197.JPG --> 20070222_194007.jpg
200702-Veggli/IMG_1198.JPG --> 20070222_194012.jpg
200702-Veggli/IMG_1200.JPG --> 20070222_194437.jpg
200702-Veggli/IMG_1201.JPG --> 20070222_194443.jpg
200702-Veggli/IMG_1202.JPG --> 20070222_194643.jpg
200702-Veggli/IMG_1203.JPG --> 20070222_194844.jpg
...

Trackback

Trackback URL for this entry: http://blog.larsstrand.org/trackback.php?id=exifdatesort

Here's what others have to say about 'Sort pictures based on Exif date':

exifsort.pl - Sort digital images by thier EXIF date. at nooblet.org
Tracked on Tuesday 13 November 2007 @ 11:07 CET

Sort pictures based on Exif date | 4 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Sort pictures based on Exif date
Authored by: Anonymous on Saturday 17 March 2007 @ 13:30 CET
I have the follow alias in my shell
perbu:~$ type jhead-iso
jhead-iso is aliased to `jhead -n%Y-%m-%d_%H:%M:%S'

Which does just about the same thing.
[ # ]
Sort pictures based on Exif date
Authored by: Anonymous on Monday 27 August 2007 @ 16:55 CEST
Thanks for the script Lars, but when I use it, the jpg's are copied in my /home directory and not in the original subdirectory.
How can I solve this little problem ?

Kalden
[ # ]
Sort pictures based on Exif date
Authored by: Anonymous on Tuesday 13 November 2007 @ 11:11 CET
I have used this as a based to create a fully featured Perl script to sort regularly updated folders into a directory structure based on the EXIF date.

Find more information at http://www.nooblet.org/exifsort
[ # ]