Pull Metadata from an Image - Python

This tutorial shows how to access metadata from an image and display it to the console in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to pull metadata from an image in a Python Console application.
Completion Time 10 minutes
Visual Studio Project Download tutorial project (1 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 Pull Metadata from an Image - 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:

Gather and Display the Metadata

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

In the Solution Explorer, open Project-Name.py and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Codecs") 
from Leadtools.Codecs import * 
 
from System.IO import * 

Add a new method in the Project-Name.py file, named gather_metadata(). This method will be called inside the main() method, below the call to the set license code. Add the code below to gather and display the metadata of the given image to the console.

def gather_metadata(): 
 
    src_file = r"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf" 
 
    codecs = RasterCodecs() 
    file_info = codecs.GetInformation(src_file, False) 
 
    print(f"Format: {file_info.Format}") 
 
    # Check if metadata is supported for the specified file format 
    is_metadata_items_supported =  RasterCodecs.MetadataItemsSupported(file_info.Format) 
    print(f"is_metadata_items_supported: {is_metadata_items_supported}") 
 
    if (is_metadata_items_supported): 
        # Read metadata items      
        metadata = codecs.ReadMetadataItems(src_file, 1) 
        if (metadata != None): 
            print(f"Items count: {metadata.Count}") 
            for item in metadata: 
                print(f"{item.Key}: {item.Value}") 

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 displays the metadata information from the sample image within the console.

Screenshot of image metadata displayed to console.

Wrap-up

This tutorial showed how to load an image and read its metadata. Also, it covered how to use the RasterCodecs class.

See Also

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


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