load image metadata with pil 1

load image metadata with pil

import PIL.Image
img = PIL.Image.open('img.jpg')
exif_data = img._getexif()

# iterating over all EXIF data fields
for tag_id in exif_data:
    # get the tag name, instead of human unreadable tag id
    tag = TAGS.get(tag_id, tag_id)
    data = exifdata.get(tag_id)
    # decode bytes 
    if isinstance(data, bytes):
        data = data.decode()
    print(f"{tag:25}: {data}")

Here is what the above code is Doing:
1. Open the image file using PIL.Image.open()
2. Get the EXIF data using the _getexif() method
3. Iterate over all EXIF data fields
4. Get the tag name, instead of human unreadable tag id
5. Decode bytes
6. Print the tag name and tag value

Similar Posts