# this program reads the GPS data from the exif metadata of a JPEG # it prints the exif data on screen # prints the GPS data separetly and converts dgrees, minutes and seconds to decimal values from PIL import Image from PIL.ExifTags import TAGS from PIL.ExifTags import GPSTAGS def get_exif(filename): image = Image.open(filename) image.verify() return image._getexif() def get_labeled_exif(exif): labeled = {} for (key, val) in exif.items(): labeled[TAGS.get(key)] = val return labeled def get_geotagging(exif): if not exif: raise ValueError("No EXIF metadata found") geotagging = {} for (idx, tag) in TAGS.items(): if tag == 'GPSInfo': if idx not in exif: print("no EXIF geotagging found!") wachten = input("tot hier OK wachten ...") raise ValueError("No EXIF geotagging found") for (key, val) in GPSTAGS.items(): #for example: 'GPSInfo': {0: b'...', 1: 'N', 2: (47.0, 13.0, 4.0), 3: 'E', 4: (9.0, 2.0, 58.0), ...} if key in exif[idx]: geotagging[val] = exif[idx][key] return geotagging exif = get_exif("20210313_171718.jpg") print(exif) print(" ") print(" ") labeled = get_labeled_exif(exif) print(labeled) print() for (idx, tag) in TAGS.items(): if tag == 'GPSInfo': if idx not in exif: #NB the idx of GPSInfo is idx=34853 print("no EXIF geotags found!") wachten = input("tot hier OK wachten ...") geotags = get_geotagging(exif) print(geotags) print() print("GPSLatitudeRef =", geotags["GPSLatitudeRef"]) print("GPSLatitude =", geotags["GPSLatitude"]) print("GPSLongitudeRef =", geotags["GPSLongitudeRef"]) print("GPSLongitude =", geotags["GPSLongitude"]) print("GPSAltitutde =", geotags["GPSAltitude"]) print() latitudedms = geotags["GPSLatitude"] longitudedms = geotags["GPSLongitude"] print(latitudedms) print(longitudedms) print() latitudedegs = float(latitudedms[0]) latitudemins = float(latitudedms[1]) latitudesecs = float(latitudedms[2]) print("Latitude:", round(latitudedegs + latitudemins/60 + latitudesecs/3600, 5)) longitudedegs = float(longitudedms[0]) longitudemins = float(longitudedms[1]) longitudesecs = float(longitudedms[2]) print("Longitude:", round(longitudedegs + longitudemins/60 + longitudesecs/3600, 5)) wachten = input("wachten ...")