Extract Image Information - Python

This tutorial shows how to gather information from various image files supported by LEADTOOLS using the CodecsImageInfo class in a Python Console application.

Overview  
Summary This tutorial covers how to use the CodecsImageInfo class in a Python application.
Completion Time 30 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 Extract Image Information - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created with the Add References and Set a License for Python tutorial.

If that project is unavailable, follow the steps in the relevant 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 Get Image Information Code

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 raster_codecs_image_info(filename). This method will be called inside the main() method below the set license code, as shown below.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License")) 
 
    raster_codecs_image_info(r"C:\LEADTOOLS23\Resources\Images\image1.cmp") 

Add the code below to the new method to gather the image information from the given file path and output it to the console.

def raster_codecs_image_info(filename): 
 
    codecs = RasterCodecs() 
    info = codecs.GetInformation(filename, True) 
    input_filename = Path.GetFileNameWithoutExtension(filename) 
    codecs_info_string = (f"Image Format: {info.Format}\n" +  
                f"Information for: {input_filename}\n" +  
                f"BitsPerPixel: {info.BitsPerPixel}\n" +  
                f"BytesPerLine: {info.BytesPerLine}\n" +  
                f"ColorSpace: {info.ColorSpace}\n" +  
                f"Byte Order: {info.Order}\n" +  
                f"Image Height: {info.Height}\n" +  
                f"Image Width: {info.Width}\n" +  
                f"Image X Resolution: {info.XResolution}\n" +  
                f"Image Y Resolution: {info.YResolution}\n" +  
                f"Compression: {info.Compression}\n" +  
                f"Page Number: {info.PageNumber}\n" +  
                f"Total Pages: {info.TotalPages}") 
    print(codecs_info_string) 

Note: There are more properties inside the CodecsImageInfo class. The snippet above showcases the most commonly used properties.

Handling Streams

If you want to load the image using memory stream, then add the following code in the raster_codecs_image_info(filename) method instead of the "info = codecs.GetInformation(filename, True)" statement:

fileData = File.ReadAllBytes(filename) 
fileStream = MemoryStream(fileData) 
info = codecs.GetInformation(fileStream, True) 

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 gathers the image information and outputs it to the console.

The application runs and the console displays the extracted information.

Wrap-up

This tutorial showed how to gather information from an image file path using the CodecsImageInfo 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.