This tutorial shows how to load and save an image file in a Python console application that uses the RasterCodecs
and RasterImage
classes.
Overview | |
---|---|
Summary | This tutorial covers how to load and save an image file using the RasterCodecs and RasterImage in a Python console application. |
Completion Time | 15 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 |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Load and Save Images - Python tutorial.
Start with a copy of the project created in this project: * Add References and Set a License for Python
If you do not have that project, follow the steps in the relevant tutorial to create it.
This tutorial requires the following DLLs:
Leadtools.dll
Leadtools.Codecs.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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 *
In the Python file add a new method called load_image(filename)
, and call it in the main
method after the Support.set_license
method. This tutorial uses the following test image C:\LEADTOOLS22\Resources\Images\image1.cmp
def main():
Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License"))
image = load_image(r"C:\LEADTOOLS22\Resources\Images\image1.cmp")
Add the below code to load the image, this will return a RasterImage
:
def load_image(filename):
codecs = RasterCodecs()
# If Wanting to load from memory
# byte = File.ReadAllBytes(filename)
# ms = MemoryStream(byte)
# ms.Position = 0
# return codecs.Load(filename)
return codecs.Load(filename)
Note
LEADTOOLS supports loading from memory streams. The commented out code above shows one way to load a stream into a RasterImage object.
In the Project-Name.py
file, add a new method named save_image(image, output_filename)
def main():
Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License"))
image = load_image(r"C:\LEADTOOLS22\Resources\Images\image1.cmp")
save_image(image, r"C:\LEADTOOLS22\Resources\Images\output.jpg")
Add the below code to export the image as JPEG to the following file path: C:\LEADTOOLS22\Resources\Images\output.jpg
def save_image(image, output_filename):
codecs = RasterCodecs()
# If wanting to save to memory
# ms = MemoryStream()
# codecs.Save(image, ms, RasterImageFormat.Jpeg, 0)
codecs.Save(image, output_filename, RasterImageFormat.Jpeg, 0)
Note
LEADTOOLS also supports saving to memory streams. The commented out code above shows how to save to a stream from a RasterImage object.
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and creates a new file in the output location specified in the save call.
This tutorial showed how to use the RasterCodecs
and RasterImage
classes to load and save images in a Python console Application.