Created
November 15, 2023 13:54
-
-
Save jcarlosroldan/8dd34798fd828976b82c7583515e8dd3 to your computer and use it in GitHub Desktop.
The script creates an HTML map showcasing images from a chosen directory.
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
from folium import Map, Marker, Popup | |
from os import listdir | |
from os.path import isfile | |
from piexif import load | |
from PIL import Image | |
from PIL.ExifTags import GPSTAGS, TAGS | |
from pillow_heif import read_heif | |
EXTENSIONS = {'jpg', 'jpeg', 'jfif', 'gif', 'png', 'webp', 'heic'} | |
PATH = 'your/path/here' | |
TAG_GPSINFO = 34853 | |
def main(): | |
map = Map() | |
for file in listdir(PATH): | |
file = PATH + file | |
if not isfile(file) or file.rsplit('.', 1)[-1].lower() not in EXTENSIONS: continue | |
try: | |
latlon = get_lat_lon(file) | |
except Exception as e: | |
print(file, e) | |
latlon = 0, 0 | |
Marker( | |
latlon, | |
popup=Popup(html='<a href="file:///%s" target="_blank"><img src="file:///%s" width="100"></a>' % (file, file), show=False) | |
).add_to(map) | |
map.save('map.html') | |
print('\a') | |
def get_lat_lon(path): | |
gps = get_exif_gps(path) | |
assert gps is not None, 'No EXIF/GPS data found' | |
lat = dms_to_decimal(*gps.get('GPSLatitude')) | |
lon = dms_to_decimal(*gps.get('GPSLongitude')) | |
if gps.get('GPSLatitudeRef') == 'S': lat *= -1 | |
if gps.get('GPSLongitudeRef') == 'W': lon *= -1 | |
return lat, lon | |
def get_exif_gps(path): | |
if path.lower().endswith('.heic'): | |
res = load(read_heif(path)[0].info['exif'])['GPS'] | |
if len(res): return { | |
'GPSLatitude': tuple(a / b for a, b in res[2]), | |
'GPSLongitude': tuple(a / b for a, b in res[4]), | |
'GPSLatitudeRef': res[1], | |
'GPSLongitudeRef': res[3] | |
} | |
image = Image.open(path) | |
res = image._getexif() | |
if res: | |
res = {TAGS.get(k, k): v for k, v in res.items()} | |
if 'GPSInfo' in res: | |
return {GPSTAGS.get(k, k): v for k, v in res['GPSInfo'].items()} | |
def dms_to_decimal(degrees, minutes=0, seconds=0): | |
return degrees + minutes / 60 + seconds / 3600 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment