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

Mary-Kate Olsen
Release: 2024-10-22 20:49:00
Original
261 people have browsed it

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:

<code class="python">import PIL.Image
img = PIL.Image.open('img.jpg')
exif_data = img._getexif()</code>
Copy after login

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:

<code class="python">import PIL.ExifTags
exif = {
    PIL.ExifTags.TAGS[k]: v
    for k, v in img._getexif().items()
    if k in PIL.ExifTags.TAGS
}</code>
Copy after login

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.

The above is the detailed content of How to Extract EXIF Data from Images Using PIL in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template