Last active
January 6, 2024 05:42
-
-
Save omz/980b0c6f9c59f6015b097b6bb17aad97 to your computer and use it in GitHub Desktop.
Geotag Photos.py
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
#!python3 | |
''' | |
This is a proof-of-concept script for tagging photos with location data on iOS. | |
* First, it shows all photos that have no location data *and* are editable. | |
* Then, a grid of photos in temporal proximity is shown. You can pick multiple photos here. | |
* When you tap Done, the photo you selected first will get tagged with the average location of the photos you picked in the second step. | |
''' | |
import photos | |
import dialogs | |
from datetime import datetime, timedelta | |
import statistics | |
# Set this to the maximum time difference between a photo without location data, and one that's used to infer its location. | |
MAX_HOURS = 2 | |
def main(): | |
assets = photos.get_assets() | |
if not assets: | |
dialogs.alert('No Photos', 'You may either have no photos on this device, or you did not grant Pythonista permission to access them.') | |
return | |
candidates = [a for a in assets if a.can_edit_properties and not a.location] | |
picked_asset = photos.pick_asset(candidates, title='Photos without Location') | |
if not picked_asset: | |
return | |
matching = [] | |
for a in assets: | |
if not a.location: | |
continue | |
if abs(a.creation_date - picked_asset.creation_date) < timedelta(hours=MAX_HOURS): | |
matching.append(a) | |
if matching: | |
loc_assets = photos.pick_asset(matching, title='Pick Photo(s) for Location', multi=True) | |
avg_lat = statistics.mean([a.location['latitude'] for a in loc_assets]) | |
avg_long = statistics.mean([a.location['longitude'] for a in loc_assets]) | |
avg_location = dict(latitude=avg_lat, longitude=avg_long) | |
picked_asset.location = avg_location | |
else: | |
dialogs.alert('No Photos Found', 'There are no photos with location data that have a timestamp within %.1f hours of this photo.' % (MAX_HOURS,), 'OK', hide_cancel_button=True) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment