Skip to content

Instantly share code, notes, and snippets.

@Ge0rges
Last active June 5, 2023 14:09
Show Gist options
  • Save Ge0rges/2bde83f915bbdfe00c22242320ec035e to your computer and use it in GitHub Desktop.
Save Ge0rges/2bde83f915bbdfe00c22242320ec035e to your computer and use it in GitHub Desktop.
Add GPS data to images from ship GPS data by time matching
"""
Add GPS data to JPG pictures based on the ship.json folder.
"""
import exifread
from gpsexif import setGPS
from os import listdir
from os.path import isfile, join
import json
from datetime import datetime, timedelta
with open("ship.json") as json_data:
data = json.load(json_data)
images_folder = "images/"
onlyfiles = [f for f in listdir(images_folder) if isfile(join(images_folder, f))]
# Modify the exif in a file
for file in onlyfiles:
if file.split(".")[-1].lower() != "jpg" and file.split(".")[-1].lower() != "jpeg":
continue
with open(images_folder + file, 'rb') as fh:
tags = exifread.process_file(fh)
photo_time = tags["EXIF DateTimeOriginal"]
photo_time = datetime.strptime(str(photo_time), "%Y:%m:%d %H:%M:%S")
if photo_time.second >= 30:
photo_time = photo_time.replace(second=0)
if photo_time.minute == 59:
photo_time = photo_time.replace(hour=photo_time.hour+1)
photo_time = photo_time.replace(minute=0)
else:
photo_time = photo_time.replace(minute=photo_time.minute+1)
else:
photo_time = photo_time.replace(second=0)
for ship_data in data["features"]:
ship_data = ship_data["properties"]
ship_time = ship_data["date"]
ship_time = datetime.strptime(ship_time, "%Y-%m-%dT%H:%M:%SZ")
# Make ship time local norway, from UTC (+2h)
ship_time += timedelta(hours=2)
if ship_time == photo_time:
setGPS(images_folder + file, [ship_data["longitude"], ship_data["latitude"]])
break
@Ge0rges
Copy link
Author

Ge0rges commented May 30, 2023

You will need to install exifread and gpsexif. Then you will need to modify writer.py from gpsexif as shown here: geoyee/GPS-EXIF#1.

Place this script in a folder which also contains ship.json and a folder called images containing all the images you wish to geotag.

This script does not handle time zones, and assumes that ship.json is in UTC, and image time is in local Norwegian time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment