Author Topic: Adding Latitude and Longitude to the jpeg files from CSV file.  (Read 395 times)

tszabon

  • Newbie
  • *
  • Posts: 3
Hi,

I have spent recently quite a lot of time trying to find a quick method for adding precise GPS coordinates to large number of jpeg files. I use Canon 5D MII (without build in GPS receiver), for surveying camera position I use Trimble Geo7x working in centimeter mode. I came across a few different ways of modyfying jpg files, but all of them damaged MakerNote tags. Fortunately exiftool can add GPS data without loosing any of original exif information. I am sharing Python code I wrote, it works for me, it might will help someone too. I am not a programmer, I don't take any responsibility for damage caused by the script. Feel free to send any improvement suggestions  :)

Code: [Select]
"""
initial condidtions:
1. folder with jpeg files named: prefix_filename.jpg (eg. V1a_IMG1234.jpg)
2. CSV file with: prefix in column H (eg. V1a , V2 , V3...), latitude value in column Q (eg. 53.1234567), logitude value in column R (eg.-6,1234567)
3. exiftool.exe and python script in the same folder
"""

import subprocess
import csv
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

root = tk.Tk()
root.withdraw()

#select jpg files
pht = filedialog.askopenfilenames(defaultextension=".jpg", title="Select JPG files")

#open CSV file
r = filedialog.askopenfilename(defaultextension=".csv", title="Select CSV file")

#open txt file or create if doesn't exist
f = open("MyArgs.txt", "a")

for c in range(len(pht)):
       
        v = str(pht[c])
        x = v.rsplit("/")
        y = (x[-1])
        s = str(v[:-4])
        z = y.rsplit("_")
        q = str(z[0])

        with open(r) as csv_file:
                csv_reader = csv.reader(csv_file, delimiter=',')

                for i, row in enumerate(csv_reader):
                           
                    #compare jpg file prefix with survey point name in CSV file       
                    if  q == str(row[7]):

                        #read lon, lat from CSV file

                        csvLat = float(str(row[16]))
                        csvLon = float(str(row[17]))

                        absLat = abs(csvLat)
                        absLon = abs(csvLon)

                        latRef = "N" if csvLat > 0 else "S"
                        lonRef = "W" if csvLon < 0 else "E"

                        f.write('-execute\n{}\n-overwrite_original\n-gpslatitude={}\n-gpslongitude={}\n-gpslatituderef={}\n-gpslongituderef={}\n-gpsaltituderef=0\n'.format(v,absLat,absLon,latRef,lonRef))
                       
                    else:
                        continue

#add closing instructions to the end of txt file
f.write("-stay_open\nFalse\n-exit\n")
f.close()

#run exiftool in batch mode
subprocess.run([r"exiftool", "-stay_open", "True" , "-@", "MyArgs.txt"])

#clear txt file
f = open("MyArgs.txt", "w")
f.write("")
f.close()

messagebox.showinfo("Info", "Finished")
« Last Edit: June 17, 2020, 12:37:55 PM by tszabon »

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 4074
Re: Adding Latitude and Longitude to the jpeg files from CSV file.
« Reply #1 on: June 17, 2020, 03:08:26 PM »
I should point out that exiftool can read GPS logs from a CSV file as long as the headers are ones that exiftool can parse.
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.