+ 10

How to get Metadata from Image using python

Any solution

19th Oct 2021, 3:12 AM
Vaibhav
Vaibhav - avatar
3 Answers
+ 2
To get started, you need to install Pillow library: pip3 install Pillow Open up a new Python file and follow along: from PIL import Image from PIL.ExifTags import TAGS Now this will only work on JPEG image files, take any image you took and test it for this tutorial (if you want to test on my image, you'll find it in the tutorial's repository): # path to the image or video imagename = "image.jpg" # read the image data using PIL image = Image.open(imagename) After reading the image using the Image.open() function, let's call the getexif() method on the image which returns image metadat # extract EXIF data exifdata = image.getexif() The problem with exifdata variable now is that the field names are just IDs, not a human-readable field name, that's why we gonna need the TAGS dictionary from PIL.ExifTags module which maps each tag ID into a human-readable text:
21st Oct 2021, 7:22 AM
Mohammad Zarei
Mohammad Zarei - avatar
+ 2
# iterating over all EXIF data fields for tag_id in exifdata: # 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}")
21st Oct 2021, 7:22 AM
Mohammad Zarei
Mohammad Zarei - avatar
+ 1
Marcel Ĺžak
22nd Oct 2021, 12:38 PM
Marcel Žåk
Marcel Žåk - avatar