Home  >  Article  >  Backend Development  >  How to Obtain EXIF Metadata from Images with Python\'s PIL Library?

How to Obtain EXIF Metadata from Images with Python\'s PIL Library?

DDD
DDDOriginal
2024-10-22 22:11:03937browse

How to Obtain EXIF Metadata from Images with Python's PIL Library?

Extracting EXIF Metadata from Images Using Python PIL

When working with images, accessing embedded metadata can provide valuable information about the image's characteristics and acquisition settings. In Python, the Pillow (PIL) library offers a convenient way to extract EXIF metadata from images.

Question: How can I access the EXIF data of an image using Python's PIL library?

Answer:

To access the EXIF data of an image using PIL, you can utilize the _getexif() protected method of the Image class:

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

The _getexif() method returns a dictionary of EXIF numeric tags. However, if you prefer the tags to be indexed by their actual string names, you can use a technique like the following:

<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 method ensures that your dictionary is indexed by the human-readable EXIF tag names, making it easier to interpret the metadata.

The above is the detailed content of How to Obtain EXIF Metadata from Images with Python\'s PIL Library?. 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