"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Extract EXIF Data from Images Using PIL in Python?

How to Extract EXIF Data from Images Using PIL in Python?

Published on 2024-11-08
Browse:438

How to Extract EXIF Data from Images Using PIL in Python?

Accessing EXIF Data in Python Using PIL

When working with images in Python, it's often useful to extract metadata stored in the Exchangeable Image File Format (EXIF). The Python Imaging Library (PIL) provides a convenient mechanism for accessing EXIF data as a dictionary.

Retrieving EXIF Data Using the _getexif() Method

To retrieve EXIF data, you can utilize the _getexif() method within PIL. Here's an example:

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

This will return a dictionary with numeric keys. Each key represents an EXIF tag ID, and the corresponding value is the associated data.

Mapping Numerical Tags to Tag Names

If you prefer indexed by the human-readable tag names instead, you can use the TAGS attribute of the PIL.ExifTags module:

import PIL.ExifTags
exif = {
    PIL.ExifTags.TAGS[k]: v
    for k, v in img._getexif().items()
    if k in PIL.ExifTags.TAGS
}

This dictionary will now contain the EXIF data indexed by tag names.

With these methods, you can easily access and interpret EXIF metadata in Python, aiding in image analysis, manipulation, and organization tasks.

Release Statement This article is reprinted at: 1729584915 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3