Batch GPS into Exif from text file

Started by Archive, May 12, 2010, 08:54:01 AM

Previous topic - Next topic

Archive

[Originally posted by catnhat on 2007-03-19 21:32:54-07]

Can someone tell me if is possible to use exiftool to insert and write the GPS data for Exif on a number of photos based on a text file that contains the photo name and GPS info.  These are historical photos which I was using Picasa to Geo Code.  Since the photos are either TIF, DNG, CR2 and CRW, Picasa creates an INI file with the coding instead of updating the EXIF.  The ini files are very simple and looks like this:
[Peru_199.tif]
geotag=-15.7772,-69.9011
[encoding]
utf8=1
[Peru_217.tif]
geotag=-15.8273,-69.993
[Peru_175.tif]
geotag=-15.7698,-69.6845
I have little background in scripting, but am willing to learn if Exiftool and Pearl can meet my need.

Archive

[Originally posted by exiftool on 2007-03-19 22:52:53-07]

This is a very simple task for a Perl script and ExifTool.  I could
give you an example script if I knew the details:  Is there a single
.ini file for all of the images?  Are the .ini file(s) in the same directory
as the images?

- Phil

Archive

[Originally posted by catnhat on 2007-03-20 04:07:50-07]

Yes.  there is 1 ini file in each directory and it contains all the GPS info for all images in the directory.  Images and ini file are in the same directory. Here is a link to a sample ini file.  
http://members.shaw.ca/darcysandy/Picasa.ini

Archive

[Originally posted by exiftool on 2007-03-20 11:59:52-07]

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.

Code:
#!/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

Archive

[Originally posted by exiftool on 2007-03-20 12:11:44-07]

I should have mentioned that the script should be placed in the same
directory as 'exiftool' so it will find the ExifTool libraries.

- Phil

Archive

[Originally posted by catnhat on 2007-03-20 18:29:09-07]

This works perfectly!  Thanks!  

As a perl script, I wasn't sure how to drag and drop the INI files to run the script (like you can with the standalone executable).  So I placed the script in a BAT file and the drag and drop works great with multiple ini files.
Thanks again!