Pillow TiffTags Module
TiffTags is a submodule in the Python Pillow (PIL) image processing library, specifically used to handle tag information in TIFF (Tagged Image File Format) image files.
TIFF is a flexible bitmap image format that supports multiple compression methods and color spaces, widely used in the professional image processing field.
In TIFF files, various attributes and metadata of the image are stored in tags, and the TiffTags module provides functionality to access and manipulate these tags.
Import method:
from PIL.TiffTags import TAGS_V2
Main Functions
The TiffTags module mainly provides the following functions:
- Define standard TIFF tags and their attributes
- Provide mapping between tag names and IDs
- Describe tag data types and value ranges
- Support custom tag handling
Core Methods Explained
Main Methods of the TiffTags Module
The following are the most commonly used methods and their descriptions in the TiffTags module:
| Method/Attribute | Description | Return Type | Example |
|---|---|---|---|
TiffTags.TAGS_V2 |
Dictionary containing all standard TIFF tags, with tag IDs as keys and tag information as values | dict | {256: (256, 'ImageWidth', 4, 1, None), ...} |
TiffTags.TAGS |
Legacy TIFF tag dictionary (kept for compatibility) | dict | Same as TAGS_V2 |
TiffTags.LOOKUP |
Dictionary mapping tag names to IDs | dict | {'ImageWidth': 256, ...} |
TiffTags.IFD |
IFD (Image File Directory) tag collection | dict | Contains IFD-related tags |
TiffTags.CUSTOM_TAGS |
Dictionary used to store custom tags | dict | User-defined |
Tag Information Structure
Each TIFF tag's information is a tuple containing 5 elements:
- Tag ID (integer)
- Tag name (string)
- Data type (integer, corresponding to TIFF specification)
- Value length (integer)
- Default value or special notes (optional)
Data Type Correspondence Table
| Type Code | Data Type | Description |
|---|---|---|
| 1 | BYTE | 8-bit unsigned integer |
| 2 | ASCII | 7-bit ASCII characters |
| 3 | SHORT | 16-bit unsigned integer |
| 4 | LONG | 32-bit unsigned integer |
| 5 | RATIONAL | Two LONGs, representing a fraction |
| 6 | SBYTE | 8-bit signed integer |
| 7 | UNDEFINED | 8-bit undefined data |
| 8 | SSHORT | 16-bit signed integer |
| 9 | SLONG | 32-bit signed integer |
| 10 | SRATIONAL | Two SLONGs, representing a signed fraction |
| 11 | FLOAT | 32-bit IEEE floating point number |
| 12 | DOUBLE | 64-bit IEEE floating point number |
Practical Application Examples
Example 1: View All TIFF Tags
Example
# Print all standard TIFF tags
for tag_id, tag_info in TAGS_V2.items():
print(f"ID: {tag_id}, Name: {tag_info[1]}, Type: {tag_info[2]}")
Example 2: Find Tag ID by Name
Example
# Find the ID of the "Artist" tag
artist_tag_id = LOOKUP.get('Artist')
print(f"Artist tag ID: {artist_tag_id}") # Output: 315
Example 3: Read TIFF File Tag Information
Example
from PIL.TiffTags import TAGS
# Open the TIFF file
with Image.open('example.tif') as img:
# Get all tags
if hasattr(img, 'tag'):
for tag_id, value in img.tag.items():
tag_name = TAGS.get(tag_id, tag_id)
print(f"{tag_name}: {value}")
Advanced Usage
Custom Tag Handling
Example
from PIL.TiffTags import TAGS_V2, CUSTOM_TAGS
# Define custom tags
CUSTOM_TAGS = {
50000: (50000, 'MyCustomTag', 2, 1, None)
}
# Merge standard tags and custom tags
ALL_TAGS = {**TAGS_V2, **CUSTOM_TAGS}
# Save the TIFF file using custom tags
img = Image.new('RGB', (100, 100))
img.save('custom.tif', tiffinfo={50000: 'CustomValue'})
FAQ
Q1: Why can't some TIFF tags be read?
A1: It may be because the tag is not defined in the standard TAGS_V2, or it uses a private tag. You can try checking the file specification or contact the file creator to obtain the tag definition.
Q2: How to know the data type of a tag?
A2: You can query the data type of each tag through the TAGS_V2 dictionary. For example,TAGS_V2[256][2]will return the data type code of the ImageWidth tag.
Q3: Can TIFF tags be modified?
A3: Yes, you can use Pillow's save method with the tiffinfo parameter to modify or add tags. However, note that not all tags can be modified; some are read-only.
Summary
The TiffTags module is an important tool in the Pillow library for handling TIFF image metadata. Through this article, you should have learned:
- How to access and query standard TIFF tags
- The data structure of tag information
- How to read and write tags in practical applications
- How to handle custom tags
After mastering this knowledge, you will be able to handle metadata information in TIFF image files more effectively.
Other Extensions