|
Thanks.
Here you go. The script takes the name of one or more .ini files as its
input and processes all image files in the same directory, adding the
GPS lat/long information. Be sure you have backups of all your
images before you modify them with this script.
#!/usr/bin/perl -w
#
# Description: Add GPS information to images specified by .ini file
#
# Syntax: script INIFILE
#
use strict;
my $exeDir;
BEGIN {
# get exe directory
$exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.';
# add lib directory at start of include path
unshift @INC, "$exeDir/lib";
}
use Image::ExifTool;
my $exifTool = new Image::ExifTool;
my $count = 0;
# loop through all .ini files
for (;;) {
# get name of next .ini file
my $file = shift;
last unless defined $file;
$file =~ tr/\\/\//; # convert backslashes to slashes
open FILE, $file or warn("Error opening $file\n"), next;
my ($dir,$name);
# get the directory of the .ini and image files
if ($file =~ /(.*\/)/) {
$dir = $1;
} else {
$dir = './';
}
# process this .ini file
while (<FILE>) {
# look for image file name
/^\[(.*?)\]/ and $name = $1, next;
# look for GPS position
/^geotag=([-+]?[.\d]+),([-+]?[.\d]+)/ or next;
unless ($name) {
warn "No file for position $1, $2\n";
next;
}
# set necessary GPS tags
my ($lat, $long) = ($1, $2);
my ($latref, $longref) = ('N','E');
$lat < 0 and $lat *= -1, $latref = 'S';
$long < 0 and $long *= -1, $longref = 'W';
$exifTool->SetNewValue(GPSLatitude => $lat);
$exifTool->SetNewValue(GPSLatitudeRef => $latref);
$exifTool->SetNewValue(GPSLongitude => $long);
$exifTool->SetNewValue(GPSLongitudeRef => $longref);
# rewrite file to temporary file
my $img = "$dir$name";
my $tmp = $img . '_tmp';
-e $tmp and warn("Temp file $tmp exists!\n"), next;
my $result = $exifTool->WriteInfo($img, $tmp);
if ($result == 1) {
if (rename $tmp, $img) {
# overwrite original file with updated file
print "$img updated\n";
++$count;
next;
} else {
print "Error renaming $tmp\n";
}
} elsif ($result == 2) {
print "$img unchanged\n";
} else {
print "Error updating $img\n";
}
unlink $tmp;
}
}
print "$count files updated\n";
# end
- Phil
|