This tutorial shows how to merge images into a single multipage file in a Python application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use the RasterCodecs class to merge images into a multipage file 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 |
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 Create a Multipage File from Multiple Images - 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 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 *
import glob
Inside the main()
method create a string array that will contain the file paths to all the CMP files in the given directory. Then, create a new string value that will contain the file path to output the single multipage TIFF file.
def main():
Support.set_license(os.path.join(DemosTools.get_root(), "C:\LEADTOOLS23\Support\Common\License"))
files = glob.glob(r"C:\LEADTOOLS23\Resources\Images" + "**/*.cmp")
multipage_file = r"C:\LEADTOOLS23\Resources\Images\merged.tif"
merge_files(files, multipage_file)
Add a new method in the Project-Name.py
file, named merge_files(files, output_file)
. This method will be called inside the main()
method as shown above. Add the code below to the new method to grab each CMP file from the given directory and append each image to a multipage TIFF file.
def merge_files(files, output_file):
codecs = RasterCodecs()
for file in files:
print(f"Adding file: {file}")
image = codecs.Load(file)
codecs.Save(image, output_file, RasterImageFormat.TifJpeg411, 0, 1, -1, 1, CodecsSavePageMode.Append)
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application loads each CMP file from the C:\LEADTOOLS23\Resources\Images
directory and appends each image to a single multipage TIFF file.
This tutorial showed how to add the necessary references to save TIFF images as well as how to merge images into a multipage file using the RasterImage
and RasterCodecs
classes.