I use the following code to extract GPS location both from JPG and HEIC files:
#coding=utf-8
from PIL import Image
from urllib.request import urlopen
from PIL.ExifTags import TAGS
from PIL.ExifTags import GPSTAGS
from pillow_heif import register_heif_opener
def get_exif(filename):
image = Image.open(filename)
image.verify()
return image._getexif()
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:
raise ValueError("No EXIF geotagging found")
for (key, val) in GPSTAGS.items():
if key in exif[idx]:
geotagging[val] = exif[idx][key]
return geotagging
register_heif_opener()
my_image='IMG_9610.HEIC'
#my_image='IMG_20210116_215317.jpg'
exif = get_exif(my_image)
labeled = get_geotagging(exif)
print(labeled)
This code works well with JPEG files, but returns the following error with HEIC:
AttributeError: _getexif
If I add the following function
def get_labeled_exif(exif):
labeled = {}
for (key, val) in exif.items():
labeled[TAGS.get(key)] = val
return labeled
and replace '_getexif()' with 'getexif()' then it works for both files, but the data is encrypted there - 'GPSInfo': 1234
and get_geotagging()
doesn't work for such exif.
How could I fix it?