lawrencedeerclub/scripts/add-geolocation-to-images.py

32 lines
1.1 KiB
Python
Raw Normal View History

2024-11-26 20:41:37 -06:00
#!/usr/bin/env nix-shell
#!nix-shell --quiet -p nix-eval-jobs -p nix -p python3 -p python3Packages.pillow -i python
import json
import os
from PIL import Image
from PIL.ExifTags import TAGS
# https://stackoverflow.com/a/78462781
GPSINFO_TAG = next(
tag for tag, name in TAGS.items() if name == "GPSInfo"
)
def decimal_coords(coords, ref):
decimal_degrees = float(coords[0]) + float(coords[1]) / 60 + float(coords[2]) / 3600
if ref == "S" or ref =='W' :
decimal_degrees = -1 * decimal_degrees
return decimal_degrees
with open('data/data.json') as f:
data = json.loads(f.read())
for i, stand in enumerate(data['stands']):
lats = []
lons = []
for view_image in stand['view']:
im = Image.open(os.path.join('images', view_image))
gpsinfo = im.getexif().get_ifd(GPSINFO_TAG)
lats.append(decimal_coords(gpsinfo[2], gpsinfo[1]))
lons.append(decimal_coords(gpsinfo[4], gpsinfo[3]))
data['stands'][i]['location'] = [sum(lats)/len(lats), sum(lons)/len(lons)] # TODO: round
print(json.dumps(data, indent=4))