Home  >  Article  >  Backend Development  >  How Do I Access EXIF Data in Python?

How Do I Access EXIF Data in Python?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-22 19:27:37629browse

How Do I Access EXIF Data in Python?

How to Access EXIF Data in Python

EXIF (Exchangeable Image File Format) data provides valuable metadata about an image, including details about the camera, settings, and even GPS coordinates. In Python, accessing EXIF data from an image is straightforward using the _getexif() method of the PIL Image class.

PIL Method:

<code class="python">import PIL.Image

img = PIL.Image.open('img.jpg')
exif_data = img._getexif()</code>

This method returns a dictionary indexed by EXIF numeric tags. If you prefer tag names as dictionary keys, you can use the following code:

<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>

The resulting dictionary now contains EXIF data indexed by tag names, providing a more intuitive way to access specific metadata.

The above is the detailed content of How Do I Access EXIF Data in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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