Skip to content Skip to sidebar Skip to footer

Error While Trying To Get The EXIF Tags Of The Image

I'm trying to get the EXIF tags of an JPG image. To do this, I'm using piexif module. The problem is that I get an error - KeyError, saying this: Traceback (most recent call last

Solution 1:

Pillow only adds the exif key to the Image.info if EXIF data exists. So if the images has no EXIF data your script will return the error because the key does not exist.

You can see what image formats support the info["exif"] data in the Image file formats documentation.

You could do something like this...

img = Image.open(file)
exif_dict = img.info.get("exif")  # returns None if exif key does not exist

if exif_dict:
    exif_data = piexif.load(exif_dict)
    altitude = exif_data['GPS'][piexif.GPSIFD.GPSAltitude]
    print(altitude)
else:
    pass
    # Do something else when there is no EXIF data on the image.

Using mydict.get("key") will return a value of None if the key does not exist where as mydict["key"] will throw a KeyError.


Post a Comment for "Error While Trying To Get The EXIF Tags Of The Image"