15

I have a camera (not a cellphone) that inserts gps exif info into pictures.

Fact is: using the "gps on" all time drains the battery. So i thought: what about taking just one picture with gps on, and them at home add this exif info to the others?

I would like to know if there are applications that you know of that can help me in this scenario: having a photo with exif information about gps, copy this same gps info to a batch of another pictures.

(I prefer Linux/Mac solutions, but I accept windows as well. I don't mind if it's a command line application.)

  • 1
    possible duplicate of [How do I batch change the date taken information in EXIF data?](http://superuser.com/questions/9492/how-do-i-batch-change-the-date-taken-information-in-exif-data) also see [Editing JPEG EXIF properties](http://superuser.com/questions/57317/editing-jpeg-exif-properties) – Ƭᴇcʜιᴇ007 Jan 11 '12 at 22:29

3 Answers3

16

Take a look at ExifTool. It is a swiss army knife of Exif info manipulation, can do what you need, among many other things. It is Windows/Linux/Mac compatible command-line tool and a Perl module as well. Free and open source:

The "-tagsFromFile" Option

A special ExifTool option allows copying tags from one file to another. The command-line syntax for doing this is "-tagsFromFile SRCFILE". Any tags specified after this option on the command line are extracted from source file and written to the destination file. If no tags are specified, then all writable tags are copied. This option is very simple, yet very powerful. Depending on the formats of the source and destination files, some of tags read may not be valid in the destination file, in which case they aren't written.

The following command will change all files in current directory and its children (recursively), copying all GPS-related tags from the file SOURCE.JPG:

exiftool −overwrite_original_in_place -r -tagsFromFile SOURCE.JPG -gps:all .

Another way to do this is to put the following into a script. First parameter passed should be the file to copy GPS coordinates from, and all other parameters are the target files to be updated:

#!/usr/bin/env bash
lon=$(exiftool -s3 -GPSLongitude "$1")
lat=$(exiftool -s3 -GPSLatitude "$1")
exiftool -GPSLongitude="$lon" -GPSLatitude="$lat" "${@:2}"
haimg
  • 22,193
  • 16
  • 79
  • 113
  • I've read about it, but I'm going to need to script a lot of stuff (with bash, python, whatever). I was looking for something already done in the terms I asked. – Somebody still uses you MS-DOS Jan 11 '12 at 22:21
  • +1 @haimg, you're posting the options I can use really put into use. (But I'm still looking for something out of the box :) – Somebody still uses you MS-DOS Jan 11 '12 at 22:24
  • 2
    [jhead](http://www.sentex.net/~mwandel/jhead) is another very useful utility that may complement ExifTool. – MikeyB Jan 11 '12 at 22:25
  • @grawity: Does ${@:2} mean multiple arguments? If so, you *solved* my problem with SIX LINES!! – Somebody still uses you MS-DOS Jan 11 '12 at 22:33
  • 1
    @SomebodystillusesyouMS-DOS: All arguments (items in the `$@` array) starting with the 2nd. Also, it's four lines now that I realized that I put a lot of unnecessary (and incorrect) stuff there. *(Also, "solved".)* – u1686_grawity Jan 11 '12 at 22:34
  • 1
    While you're using the script, I'll keep facedesking over the fact that I totally missed the `tagsFromFile` option which could've done the same in one line... – u1686_grawity Jan 11 '12 at 22:41
  • 1
    @grawity: I was a bit surprised by your edit :-) – haimg Jan 11 '12 at 22:50
  • 2
    @grawity and @haimg: `exiftool −overwrite_original_in_place -r -tagsFromFile SOURCE.JPG -gps:all .` - the `-r` option recurses into the directory (`.`), and the `-gps:all`, well... it's just what I was looking for. I beat you, grawity! :) I think you can edit this post and add this snippet, but remember to warn people to read `exiftool --help` to understand the other options I gave... thank you all! – Somebody still uses you MS-DOS Jan 12 '12 at 04:27
  • This seems to modify some other tags, such as `Jpg From Raw Start`, `Strip Offsets`, and `Other Image Start`. Should this be of any concern? – Skeleton Bow Jul 05 '18 at 19:46
  • The last bash script got lon = "84 deg 18' 5.42" W" but wrote "84 deg 18' 5.42" E" placing the image somewhere in china. I assume this is a double quote problem. – wcochran Dec 17 '21 at 17:25
  • Actually this is a bug in `exiftool`. It reads "84 deg 18' 5.42" W" but writes "84 deg 18' 5.42" E". That is crappy. – wcochran Dec 17 '21 at 18:07
  • Added solution that bypasses and explains bug. – wcochran Dec 17 '21 at 19:13
1

You can also use exiv2 - it's much faster and, for example, can write exif data to webp images (and others).

exiv2 -PkV --grep GPSL source.jpg | exiv2 -m- destination.webp

This is example from exiv2 board.

0

There is a bug in exiftool where reading and writing the longitude is not consistent. So I wrote a Perl script to do it another way that bypasses the bug (see bug description below script):

#!/usr/bin/perl

use strict;

die "usage: src.jpg dst.jpg" unless @ARGV == 2;
my ($SRC, $DST) = ($ARGV[0], $ARGV[1]);

my @GPS = `exiftool -c "%.12f" -gpslatitude -gpslongitude $SRC`;
my $N = @GPS;
die "No GPS data in $SRC\n" unless $N >= 2;
my ($LAT,$LON,$NS,$EW) = (0,0,"N","W");
if ($GPS[0] =~ /Lat[^\d]*(\d+\.\d+?)\ ([NS])/) {
    $LAT = $1;
    $NS = $2;
}
if ($GPS[1] =~ /Lon[^\d]*(\d+\.\d+)\ ([EW])/) {
    $LON = $1;
    $EW = $2;
}
    
my $LATREF = $NS eq "N" ? "North" : "South";
my $LONREF = $EW eq "E" ? "East" : "West";

my $CMD = "exiftool -GPSLatitude=$LAT -GPSLongitude=$LON -GPSLongitudeRef=\"$LONREF\" -GPSLatitudeRef=\"$LATREF\" \"$DST\"";
system($CMD) == 0 or die "$!\n";

By the way, the exiftool bug is here on the following line

$deg = -$deg if $doSign ? $val =~ /[^A-Z](S(outh)?|W(est)?)\s*$/i : $deg < 0;

which fails when $doSign is undef which is what is passed on the line

PrintConvInv => 'Image::ExifTool::GPS::ToDegrees($val,undef,"lat")',
wcochran
  • 101
  • 2