深度阅读

How to access JPEG image metadata

作者
作者
2023年08月22日
更新时间
10.15 分钟
阅读时间
0
阅读量

To access JPEG image metadata, including EXIF data, in Python, you can use the Pillow library, which is a fork of the popular PIL (Python Imaging Library) library. Here’s an example of how to use Pillow to access the metadata of a JPEG image:

from PIL import Image

# Open the image
image = Image.open("example.jpg") # replace "example.jpg" with the name of your JPEG file

# Get the metadata
metadata = image.getexif()

# Print the metadata
for tag, value in metadata.items():
    print(f"Tag: {tag} \t Value: {value}")

In this example, we first open the JPEG image using Pillow‘s Image.open() method. Next, we call the getexif() method of the Image object to get the EXIF data as a dictionary. Finally, we loop through the dictionary to print the metadata items.

Note that the metadata returned by getexif() is in the form of tag-value pairs, where each tag is represented by an integer ID. You can look up the names of these tags using the ExifTags module in the Pillow library.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。