How Can I Access EXIF Metadata as a Dictionary in Python Using PIL?

Linda Hamilton
Release: 2024-10-22 22:08:03
Original
431 people have browsed it

How Can I Access EXIF Metadata as a Dictionary in Python Using PIL?

Retrieving EXIF Metadata as a Dictionary in Python

In image processing, extracting metadata from images like EXIF data can provide valuable information. This article explores how to access EXIF data stored in an image using the Python Imaging Library (PIL).

How to Convert EXIF Data to a Dictionary Using PIL

To obtain EXIF data as a dictionary in PIL, follow these steps:

Step 1: Open the Image

First, import the Image module from PIL and open the image of interest:

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

Step 2: Access EXIF Data

Next, utilize the protected method _getexif() to extract the EXIF data:

<code class="python">exif_data = img._getexif()</code>
Copy after login

This results in a dictionary with keys representing EXIF numeric tags and values representing corresponding data.

Step 3: Map EXIF Tags to Strings (Optional)

To obtain a dictionary indexed by human-readable EXIF tag names, map the numeric tags to their corresponding strings from the PIL.ExifTags module:

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

This gives you a more user-friendly representation of the EXIF metadata in dictionary form.

The above is the detailed content of How Can I Access EXIF Metadata as a Dictionary in Python Using PIL?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!