Sometimes, we want to read the exif data for an image in Python.
In this article, we’ll look at how to read the exif data for an image in Python.
How to read the exif data for an image in Python?
To read the exif data for an image in Python, we can use PIL.
For instance, we write
from PIL import Image, ExifTags
img = Image.open("sample.jpg")
img_exif = img.getexif()
if img_exif is None:
print('Sorry, image has no exif data.')
else:
for key, val in img_exif.items():
if key in ExifTags.TAGS:
print(f'{ExifTags.TAGS[key]}:{val}')
to open the image with Image.open
.
Then we call img.getexif
to get the exif data from the image.
Next, we loop through the img_exif
dict with a for loop.
We get the key and value with items
.
Conclusion
To read the exif data for an image in Python, we can use PIL.