#!/usr/bin/python import sys, os def usage(rc=0): print """gpstag lat lon file... -- Insert or update GPS info in a JPG file lat and lon can be given as a float or as "degrees minutes seconds", in which case the arguments need to be quoted.""" sys.exit(rc) def dec2exif(dec): deg = int(dec) dec -= deg; dec *= 60 min = int(dec) sec = (dec - min) * 6000 return "%d/1 %d/1 %d/100" % (deg, min, sec) def str2f(s): if not ' ' in s: return float(s) s = (s.split() + ['0', '0'])[:3] # zero-pad on the right sign = (1, -1)[s[0].startswith('-')] return sign * (abs(float(s[0])) + float(s[1]) / 60. + (float(s[2]) + .5) / 3600.) def main(args): if len(args) < 3: usage(1) try: lat = str2f(args[0]) lon = str2f(args[1]) except (ValueError, IndexError): usage(1) cmd = """exiv2 -k \ -M"set Exif.GPSInfo.GPSMapDatum WGS-84" \ -M"set Exif.GPSInfo.GPSVersionID 2 2 0 0" \ -M"set Exif.GPSInfo.GPSLatitude %s" \ -M"set Exif.GPSInfo.GPSLatitudeRef %s" \ -M"set Exif.GPSInfo.GPSLongitude %s" \ -M"set Exif.GPSInfo.GPSLongitudeRef %s" '%%s'""" % ( dec2exif(abs(lat)), "NS"[lat < 0], dec2exif(abs(lon)), "EW"[lon < 0] ) for f in args[2:]: os.system(cmd % f) if __name__ == '__main__': main(sys.argv[1:]) sys.exit(0)