2007-01-28

what time is it, redux?

Well, after my last vacation where every photo taken by my Canon PowerShot SD800 IS Digital ELPH was off exactly by one day, I hunkered down and wrote the code to edit the meta information in all the photos using Image::ExifTool. Below is the code:

#!/usr/bin/perl -w
use strict;
use Image::ExifTool qw(:Public);
use Time::Local;
open(F,"list"); my @files = ; close(F);
my $oneday = 60 * 60 * 24;
foreach my $file (@files) {
chomp $file;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks)
= stat($file);
my $newmtime = $mtime + $oneday;
# Create a new Image::ExifTool object
my $exifTool = new Image::ExifTool;
my %options;
# Extract meta information from an image
$exifTool->ExtractInfo($file, \%options);
# Get list of tags in the order they were found in the file
my @taglist = $exifTool->GetFoundTags('File');
TAG: foreach my $tag (@taglist) {
next TAG if $tag !~ /date/i;
# Get a tag description
my $description = $exifTool->GetDescription($tag);
# Get the group name associated with this tag
my $group = $exifTool->GetGroup($tag);
# Get the value of a specified tag
my $value = $exifTool->GetValue($tag);
# 2007:01:05 12:26:43
my ($year,$mon,$mday,$hour,$min,$sec) = $value =~ /(\d\d\d\d):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)/;
$mon -= 1;
my $oldtime = timelocal($sec,$min,$hour,$mday,$mon,$year);
# let's do the time warp agaaaaain
my $newtime = $oldtime + $oneday;
my ($newsec,$newmin,$newhour,$newmday,$newmon,$newyear,$newwday,$newyday,$newisdst) = localtime($newtime);
$newmon += 1;
$newyear += 1900;
my $newvalue = sprintf("%04d:%02d:%02d %02d:%02d:%02d",$newyear,$newmon,$newmday,$newhour,$newmin,$newsec);
# set a new value and capture any error message
my ($success, $errStr) = $exifTool->SetNewValue($tag, $newvalue, Replace => 1);
if ( ! $success > 0 ) {
print "***\n";
print qq!ERROR: $file - $tag ($description) $group '$errStr'\n!;
print "***\n";
}
}
$exifTool->WriteInfo($file);
}

Labels:

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home