Pillow TiffTags Module
TiffTags is a submodule of the Python Pillow (PIL) image processing library, specifically for handling 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, and is widely used in professional image processing.
In TIFF files, various image attributes and metadata 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 Function
The TiffTags module mainly provides the following features:
- Define standard TIFF tags and their attributes
- Provide mappings between tag names and IDs
- Describe the data types and value ranges of tags
- Support handling of custom tags
Core Method Details
Main Methods of the TiffTags Module
The following are the most commonly used methods in the TiffTags module and their descriptions:
| Method/attribute | Description | Return value type | Example |
|---|---|---|---|
TiffTags.TAGS_V2 |
A 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 (retained for compatibility) | dict | Same as TAGS_V2 |
TiffTags.LOOKUP |
A dictionary mapping tag names to IDs | dict | {'ImageWidth': 256, ...} |
TiffTags.IFD |
A collection of IFD (Image File Directory) tags | dict | Contains IFD-related tags |
TiffTags.CUSTOM_TAGS |
A dictionary for storing 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 the TIFF specification)
- Value length (integer)
- Default value or special note (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
from
PIL.
TiffTags
import
TAGS_V2
Print all standard TIFF tags
for
tag_id
,
tag_info
in
TAGS_V2.
items
(
)
:
(
f
"ID: {tag_id}, Name: {tag_info[1]}, Type: {tag_info[2]}"
)
Example 2: Find a tag ID by name
Example
from
PIL.
TiffTags
import
LOOKUP
Find the ID of the "Artist" tag
artist_tag_id
=
LOOKUP.
get
(
'Artist'
)
(
f
"Artist tag ID: {artist_tag_id}"
)
Output: 315
Example 3: Read TIFF file tag information
Example
from
PIL
import
Image
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
)
(
f
"{tag_name}: {value}"
)
Advanced usage
Custom Tag Handling
Example
from
PIL
import
Image
from
PIL.
TiffTags
import
TAGS_V2
,
CUSTOM_TAGS
Define a custom tag
CUSTOM_TAGS
=
{
50000
:
(
50000
,
'MyCustomTag'
,
2
,
1
,
None
)
}
Merge standard tags and custom tags
ALL_TAGS
=
{
**TAGS_V2
,
**CUSTOM_TAGS
}
Save a TIFF file with custom tags
img
=
Image.
new
(
'RGB'
,
(
100
,
100
)
)
img.
save
(
'custom.tif'
,
tiffinfo
=
{
50000
:
'CustomValue'
}
)
Frequently Asked Questions
Q1: Why can some TIFF tags not be read?
A1: This may be because the tag is not defined in the standard TAGS_V2, or a private tag is used. You can try checking the file specification or contacting the file creator to obtain the tag definition.
Q2: How do you know a tag's data type?
A2: The data type of each tag can be queried through the TAGS_V2 dictionary. For example,TAGS_V2[256][2]returns 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. But note that not all tags are modifiable; 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
With this knowledge, you will be able to handle metadata in TIFF image files more effectively.