This tutorial shows how to save each page of a multipage image into separate image files in a Python application using the LEADTOOLS SDK.
Overview | |
---|---|
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 |
|
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.
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:
Leadtools.dll
Leadtools.Codecs.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
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
from leadtools import LibraryLoader
LibraryLoader.add_reference("Leadtools")
from Leadtools import *
LibraryLoader.add_reference("Leadtools.Codecs")
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:/LEADTOOLS23/Support/Common/License"))
multipage_file = r"C:\LEADTOOLS23\Resources\Images\addcommand.tif"
split_file(multipage_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:\LEADTOOLS23\Resources\Images\{Path.GetFileNameWithoutExtension(input_file)}_page{page}.png"
image = codecs.Load(input_file, page)
codecs.Save(image, output_file, RasterImageFormat.Png, 0)
Note: 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 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.