Here is the script I am Using Python 3:
from print import print
from PIL import Image
import piexif
codec = 'ISO-8859-1' # or latin-1
def exif_to_tag(exif_dict):
exif_tag_dict = {}
thumbnail = exif_dict.pop('thumbnail')
exif_tag_dict['thumbnail'] = thumbnail.decode(codec)
for ifd in exif_dict:
exif_tag_dict[ifd] = {}
for tag in exif_dict[ifd]:
try:
element = exif_dict[ifd][tag].decode(codec)
except AttributeError:
element = exif_dict[ifd][tag]
exif_tag_dict[ifd][piexif.TAGS[ifd][tag]["name"]] = element
return exif_tag_dict
def main():
filename = 'subb.jpeg' # obviously one of your own pictures
im = Image.open(filename)
exif_dict = piexif.load(im.info.get('exif'))
exif_dict = exif_to_tag(exif_dict)
pprint(exif_dict['GPS'])
if __name__ == '__main__':
main()e here
Output is:
{'GPSDateStamp': '2022:09:04',
'GPSLatitude': ((43, 1), (35, 1), (28845, 1277)),
'GPSLatitudeRef': 'N',
'GPSLongitude': ((13, 1), (12, 1), (30645, 1024)),
'GPSLongitudeRef': 'E'}
The formula to convert DMS to DD is described by ESRI as:
Also note that depending on orientation, you might need to make your numbers negative:
But before that,
GPSLatitudeandGPSLongitudeare stored as 3 tuples of rational numbers, each of which are in the format of(numerator, denominator), so you have to do some division to get the values.Using your image's EXIF GPS metadata, here's code that can calculate DMS.
And your sample EXIF coordinates return:
Which, if my math is correct, is somewhere in Italy.