This tutorial shows how to burn annotations onto an image in a Python application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers burning annotations to an image in a Python application. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (497 KB) |
Platform | Python Application |
IDE | Visual Studio 2022 |
Runtime Target | Python 3.10 or Higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
Before working on the Burn Annotations to an Image - Python tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License 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.
This tutorial requires the following DLLs:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.Rendering.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
# Add references to LEADTOOLS
from leadtools import LibraryLoader
LibraryLoader.add_reference("Leadtools")
from Leadtools import *
LibraryLoader.add_reference("Leadtools.Codecs")
from Leadtools.Codecs import *
LibraryLoader.add_reference("Leadtools.Annotations.Rendering")
from Leadtools.Annotations.Rendering import *
LibraryLoader.add_reference("Leadtools.Annotations.Engine")
from Leadtools.Annotations.Engine import *
from System.IO import *
In the Project-Name.py
file, add a new method called burn_annotations_to_image()
and call it inside the main()
method after the Support.set_license
method. The source image is provided with the project. There is also an XML file that contains the annotations data. The file names for these two files are:
File Name | Description |
---|---|
Burn-Annotations-to-an-Image-Source-Image.jpg | Image file |
Burn-Annotations-to-an-Image-Annotations-File.xml | LEAD annotations file |
These files can be found in the downloadable project for this tutorial.
Add the below code to load a RasterImage, load the AnnContainer, map the AnnContainer to the image, burn the container to the image, and export the new image to a file.
def burn_annotations_to_image():
image_file = r"Burn-Annotations-to-an-Image-Source-Image.jpg"
ann_file = r"Burn-Annotations-to-an-Image-Annotations-File.xml"
output_file = r"output.jpg"
rendering_engine = AnnDrawRenderingEngine()
codecs = RasterCodecs()
ann_codecs = AnnCodecs()
ann_container = AnnContainer()
src_image = codecs.Load(image_file)
# Uncomment to use Memory Stream to load file:
# bytes = File.ReadAllBytes(image_file)
# ms = MemoryStream(bytes)
# ms.Position = 0;
# codecs.Load(ms)
# print("Image loaded")
ann_container.Mapper.MapResolutions(src_image.XResolution, src_image.YResolution, src_image.XResolution, src_image.YResolution)
ann_container.Size = ann_container.Mapper.SizeToContainerCoordinates(src_image.ImageSize.ToLeadSizeD())
ann_container = ann_codecs.Load(ann_file, 0)
# Uncomment to use Memory Stream to handle the files:
# outputBytes = File.ReadAllBytes(output_file)
# ms = MemoryStream(outputBytes)
burn_image = rendering_engine.RenderOnImage(ann_container, src_image)
codecs.Save(burn_image, output_file, RasterImageFormat.Jpeg, 0)
path = Path.GetFullPath(output_file)
print("Image Saved", path)
# Uncomment to save using Memory Stream:
# ms.Position = 0
# codecs.Save(burn_image, ms, RasterImageFormat.Jpeg, 0)
# path = Path.GetFullPath(output_file)
# print("Image saved", path)
To load an image using memory stream, uncomment the code located in the burn_annotations_to_image()
method.
# Use Memory Stream to load file:
bytes = File.ReadAllBytes(image_file)
ms = MemoryStream(bytes)
ms.Position = 0;
codecs.Load(ms)
print("Image loaded")
# Use Memory Stream to handle the files:
outputBytes = File.ReadAllBytes(output_file)
ms = MemoryStream(outputBytes)
# Save using Memory Stream:
ms.Position = 0
codecs.Save(burn_image, ms, RasterImageFormat.Jpeg, 0)
path = Path.GetFullPath(output_file)
print("Image saved", path)
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application loads the specified image, loads the specified annotations XML file, and then burns those annotations to the image and exports that image to a file. The following screenshot shows the expected output:
This tutorial showed how to add the necessary references to burn annotations to an image, and how to use the AnnCodecs
, AnnContainer
, and AnnDrawRenderingEngine
classes.