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:

2006-10-02

what time is it?

No time for home tech projects lately... been too busy at work. The only thing of note was a minor perl script that went through my digital picture archives looking for images that accidentally had a timestamp of 01/01/2000 00:00:00. That's the timestamp my Olympus C-3030Zoom gives a pic after battery ran out and it's forgotten the date/time. I didn't actually change the JPEG timestamp that's builtin to the image, just the filesystem mtime. That's a whole other perl script and some fancy perl modules. Here's the meat-n-potatoes of the script:

my ($fmonth,$fday,$num,$ext) = $dirent =~ /P(.)(..)(....)(\.jpg)/i;
if ( ( $fmonth eq "1" && $fday eq "01") && ($month != 1 && $day != 1) ) {
my $newdirent = sprintf("P%1X%02d%04d%s",$month,$day,$num,$ext);
print "should rename $dir/$dirent to $dir/$newdirent\n";
`mv $dir/$dirent $dir/$newdirent`;
$dirent=$newdirent;
}
$num += 0;
my $hour = 12 + int($num / 60);
my $min = $num % 60;
my $sec = 0;
my $time = Mktime($year,$month,$day, $hour,$min,$sec);
my $dstring = localtime($time);
my $f = "$dir/$dirent";
print "timestamp '$f' as $dstring\n";
utime $time, $time, $f

Labels: