Home >Backend Development >Python Tutorial >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>
Step 2: Access EXIF Data
Next, utilize the protected method _getexif() to extract the EXIF data:
<code class="python">exif_data = img._getexif()</code>
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>
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!