Split a Multipage Image File Into Separate Files - Python

This tutorial shows how to save each page of a multipage image into separate image files in a Python application using the LEADTOOLS SDK.

Summary This tutorial covers how to split multipage image files using the RasterCodecs class in a Python Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 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 Split a Multipage Image File Into Separate Files - 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 Split Image 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 
from Leadtools import * 
from Leadtools.Codecs import * 
from System.IO import *  

In the Project-Name.py file, add a new string multipage_file, below the set license code. Set the new string value to the file path containing the multipage image file. Add a new method named split_file(input_file) to the Project-Name.py file. Call it in the main() method after the multipage_file string value, as shown below.

def main(): 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
    multipage_file = r"FILE PATH TO MULTIPAGE IMAGE FILE" 

Add the code below to the split_file() method to load each page individually from a multipage image file and export the page as its own image file.

def split_file(input_file): 
    codecs = RasterCodecs() 
    total_pages = codecs.GetTotalPages(input_file) 
    for page in range(1, total_pages): 
        output_file = fr"C:\LEADTOOLS22\Resources\Images\{Path.GetFileNameWithoutExtension(input_file)}_page{page}.png" 
        image = codecs.Load(input_file, page) 
        codecs.Save(image, output_file, RasterImageFormat.Png, 0) 


To test the code above, use a multipage file, such as TIFF or PDF. If such file is not available, create one by following the Create a Multipage File from Multiple Images tutorial.

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 creates new PNG image files from each page of the multipage image file.


This tutorial showed how to add the necessary references to load all the pages of a TIFF image file and split them into separate PNG images using the RasterImage and RasterCodecs classes.

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.