Pillow ExifTags Module
ExifTags is a submodule of the Python Pillow image processing library, specifically used to handle EXIF (Exchangeable Image File Format) metadata in images.
EXIF data is additional information embedded in image files, typically including the capture device, shooting parameters (such as aperture, shutter speed), GPS location, and other information.
Import ExifTags:
from PIL import Image, ExifTags
Why Do You Need the ExifTags Module
- Read image metadata: Understand the shooting information and attributes of the image
- Image processing: Perform image processing based on EXIF data (e.g., auto-rotation)
- Data analysis: Batch analyze the shooting parameters of photos
- Privacy protection: View or delete sensitive location information
Main Methods of the ExifTags Module
The following table lists the most commonly used methods and attributes in the ExifTags module:
| Method/Attribute | Description | Return Value Type | Example |
|---|---|---|---|
TAGS |
Dictionary containing the mapping from all standard EXIF tag IDs to names | dict | {256: 'ImageWidth', 257: 'ImageLength'} |
GPSTAGS |
Dictionary containing the mapping from all GPS-related EXIF tag IDs to names | dict | {0: 'GPSVersionID', 1: 'GPSLatitudeRef'} |
get(tag_id) |
Get the tag name based on the tag ID | str | ExifTags.get(36867) → 'DateTimeOriginal' |
getid(tag_name) |
Get the tag ID based on the tag name | int | ExifTags.getid('DateTimeOriginal') → 36867 |
Usage Examples
1. View All EXIF Tags
Example
from PIL import Image, ExifTags
# Print all standard EXIF tags
for tag_id in ExifTags.TAGS:
tag_name = ExifTags.TAGS[tag_id]
print(f"ID: {tag_id}, Name: {tag_name}")
# Print all standard EXIF tags
for tag_id in ExifTags.TAGS:
tag_name = ExifTags.TAGS[tag_id]
print(f"ID: {tag_id}, Name: {tag_name}")
2. Read Image EXIF Data
Example
from PIL import Image, ExifTags
image = Image.open("example.jpg")
# Get EXIF data
exif_data = image._getexif()
if exif_data:
for tag_id, value in exif_data.items():
# Get tag name
tag_name = ExifTags.TAGS.get(tag_id, tag_id)
print(f"{tag_name:25}: {value}")
else:
print("The image has no EXIF data")
image = Image.open("example.jpg")
# Get EXIF data
exif_data = image._getexif()
if exif_data:
for tag_id, value in exif_data.items():
# Get tag name
tag_name = ExifTags.TAGS.get(tag_id, tag_id)
print(f"{tag_name:25}: {value}")
else:
print("The image has no EXIF data")
3. Handle GPS Information
Example
from PIL import Image, ExifTags
image = Image.open("with_gps.jpg")
exif_data = image._getexif()
if exif_data:
# Get GPS information
gps_info = {}
for tag_id in exif_data:
tag_name = ExifTags.TAGS.get(tag_id, tag_id)
if tag_name == "GPSInfo":
for gps_tag_id in exif_data[tag_id]:
gps_tag_name = ExifTags.GPSTAGS.get(gps_tag_id, gps_tag_id)
gps_info[gps_tag_name] = exif_data[tag_id][gps_tag_id]
print("GPS information:", gps_info)
image = Image.open("with_gps.jpg")
exif_data = image._getexif()
if exif_data:
# Get GPS information
gps_info = {}
for tag_id in exif_data:
tag_name = ExifTags.TAGS.get(tag_id, tag_id)
if tag_name == "GPSInfo":
for gps_tag_id in exif_data[tag_id]:
gps_tag_name = ExifTags.GPSTAGS.get(gps_tag_id, gps_tag_id)
gps_info[gps_tag_name] = exif_data[tag_id][gps_tag_id]
print("GPS information:", gps_info)
Common EXIF Tags
The following are several commonly used EXIF tags and their IDs:
| Tag ID | Tag Name | Description |
|---|---|---|
| 271 | Make | Camera manufacturer |
| 272 | Model | Camera model |
| 274 | Orientation | Image orientation |
| 282 | XResolution | Horizontal resolution |
| 283 | YResolution | Vertical resolution |
| 296 | ResolutionUnit | Resolution unit |
| 306 | DateTime | Modification date and time |
| 36867 | DateTimeOriginal | Original capture time |
| 36868 | DateTimeDigitized | Digitization time |
| 33434 | ExposureTime | Exposure time |
| 33437 | FNumber | Aperture value |
Notes
- Not all images have EXIF data: Some images may have been processed and had EXIF information removed
- Privacy concerns: GPS location information may leak privacy; consider deleting it before sharing images
- Write limitations: Pillow has limited support for writing EXIF data; other libraries may be needed for complex operations
- Data types: Values in EXIF data can be of various types (strings, tuples, rational numbers, etc.)
By mastering the ExifTags module, you can easily access and process metadata in images, providing more possibilities for image processing and analysis.
Other Extensions