Read and Write TIFF Tags and Comments - Python

This tutorial shows how to read and write TIFF tags and comments in a Python application using the LEADTOOLS SDK.

Summary This tutorial covers how to use the RasterCommentMetaData class in a Python application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 3.10 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Read and Write TIFF Tags and Comments - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project.

This tutorial requires the following .NET DLLs:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Read/Write TIFF Comments Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
from Leadtools import * 
from Leadtools.Codecs import * 

In the file, add three new methods named read_and_write_tif_comments(filename), read_and_write_tif_tags(filename), and read_and_write_tags_to_multipage_tif(multipage_tif) to the file. Call it in the main() method as shown below.

def main(): 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
    filename = r"C:\LEADTOOLS22\Resources\Images\clean.tif" 
    multipage_tif = r"FILEPATH TO MULTIPAGE TIFF" 

Add the code below to the read_and_write_tif_comments() method to write a comment to the TIFF file, then read the comment and display it to the console.

def read_and_write_tif_comments(filename): 
    codecs = RasterCodecs() 
    # Write the comment to the file 
    write_comment = RasterCommentMetadata() 
    write_comment.Type = RasterCommentMetadataType.Software 
    write_comment.FromAscii("LEADTOOLS Demo") 
    codecs.WriteComment(filename, 1, write_comment) 
    # Read the comment 
    read_comment = codecs.ReadComment(filename, 1, RasterCommentMetadataType.Software) 
    print(f"The following comment has been read: {read_comment.ToAscii()}/n") 

Add the Read/Write TIFF Tags Code

Add the below code to the read_and_write_tif_tags() method to read the XResolution of the TIFF image, modify the value, and write the tag to the file.

def read_and_write_tif_tags(filename): 
    codecs = RasterCodecs() 
    # This reads the Xresolution from a TIFF image, modifies the value, and saves it back 
    xres_tag_id = 282 
    read_tag = codecs.ReadTag(filename, 1, xres_tag_id) 
    rational = read_tag.ToURational() 
    rational[0].Numerator = rational[0].Numerator * 5 
    rational[0].Denominator = rational[0].Denominator * 1 
    codecs.WriteTag(filename, 1, read_tag) 
    print("Resolution changed successfully\n")  

Add Read/Write support for Multipage TIFF files

Add the below code to the read_and_write_tags_to_multipage_tif() method to read the PageName for each page in a MultiPage TIFF file, and then save the values in a new file.

def read_and_write_tags_to_multipage_tif(multipage_tif): 
    if (multipage_tif != ""): 
        codecs = RasterCodecs() 
        tiff_tag_page_name = 285 
        image_info = codecs.GetInformation(multipage_tif, True) 
        total_pages = image_info.TotalPages 
        codecs.Options.Load.Tags = True 
        codecs.Options.Save.Tags = True 
        for i in range(1, total_pages+1): 
            raster_image = codecs.Load(multipage_tif, i)  
            codecs.Save(raster_image, "output.tif", RasterImageFormat.TifJpeg411, 0, 1, 1, 1, CodecsSavePageMode.Append) 
            # Check if the page has a page name 
            if (codecs.ReadTag(multipage_tif, i, tiff_tag_page_name) != None):  
                    print("Input page name:", codecs.ReadTag(multipage_tif, i, tiff_tag_page_name).ToAscii()) 
                    codecs.WriteTag("output.tif", i, codecs.ReadTag(multipage_tif, i, tiff_tag_page_name)) 
            # Create a new page name if the page was missing a page name.       
                print("No page name found, adding new page name") 
                tag = RasterTagMetadata() 
                tag.Id = tiff_tag_page_name 
                tag.DataType = RasterTagMetadataDataType.Ascii;  
                tag.FromAscii(f"Page #{i}");  
                codecs.WriteTag("output.tif", i, tag) 
            print("Output page name:", codecs.ReadTag("output.tif", i, tiff_tag_page_name).ToAscii())     

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application writes a new TIFF comment and then displays the comment in the console. Then, the application reads the XResolution from the TIFF image, modifies the resolution value and writes the tag back to the TIFF image. The application will also write TIFF comments to a new multipage TIFF if one is provided.


This tutorial showed how to use the RasterTagMetadata and RasterCommentMetadata classes to read/write TIFF comments and tags.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.