Python 3: working with files and digital assets/Image/EXIF data

EXIF Data

edit

Requirement

edit
%pip install pillow


pip install pillow

Retrieval of data

edit

Code Snippet[1]:

from PIL import Image
from PIL.ExifTags import TAGS as exif_tags

# location of the image you want to extract the exif information from
location_of_file: str = ''

def retrievement_of_exif_table(
        exif_information
    ) -> dict:
    exif_table: dict = {}

    for key, value in exif_information.items():
        exif_label = exif_tags.get(key)
                
        if not(
            exif_label is None
        ):
            exif_table[exif_label] = value
    
    return exif_table

exif_table = {}

with Image.open(location_of_file) as image:
    exif_table = retrievement_of_exif_table(
        image.getexif()
    )

Retrieval of GPS Location

edit

Code snippet[1]:

from PIL.ExifTags import GPS, GPSTAGS

# .....
def convert_gps_information(
    information
) -> dict:
    gps_information: dict = {}

    for key in information.keys():
        gps_label = GPSTAGS.get(key)
        gps_information[gps_label] = information[key]

    return gps_information


def retrievement_of_exif_table(
        exif_information
    ) -> dict:
    # .....
    for key, value in exif_information.items():
        exif_label = exif_tags.get(key)
        
        if exif_label == 'GPSInfo':
            value = convert_gps_information(
                exif_information.get_ifd(
                    key
                )
            )
    # .....


Footnotes

edit
  1. a b "image exif information tutorial.ipynb". Gist. Retrieved 2023-07-12.