Last active
June 5, 2023 14:09
-
-
Save Ge0rges/2bde83f915bbdfe00c22242320ec035e to your computer and use it in GitHub Desktop.
Add GPS data to images from ship GPS data by time matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will need to install
exifread
andgpsexif
. Then you will need to modifywriter.py
fromgpsexif
as shown here: geoyee/GPS-EXIF#1.Place this script in a folder which also contains
ship.json
and a folder calledimages
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.