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.
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>
This will return a dictionary with numeric keys. Each key represents an EXIF tag ID, and the corresponding value is the associated data.
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>
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!