Recognize Handwritten Text From Images With ICR - Python

This tutorial shows how to run ICR on an image in a Python application using the LEADTOOLS SDK.

Summary This tutorial covers how to use OcrEngineType to run LEADTOOLS ICR on an image 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 3.10 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 Recognize Handwritten Text From Images With ICR - 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:

Initialize the IOcrEngine and Add ICR Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open 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.Ocr import * 
from Leadtools.Document.Writer import * 

Add a new method to the file named run_icr(). Call the run_icr() method inside the main() method below the set license code, as shown below.

def main(): 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 

Add the code below to the run_icr() method to run ICR (Intelligent Character Recognition) on the loaded image and export the results as a searchable PDF. For the purposes of this tutorial, the sample image in the following file path is used: <INSTALL_DIR>\LEADTOOLS22\Resources\Images\demoicr2.tif.

def run_icr(): 
    file = r"C:\LEADTOOLS22\Resources\Images\demoicr2.tif" 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime") 
    ocr_document = ocr_engine.DocumentManager.CreateDocument() 
    ocr_page = ocr_document.Pages.AddPage(file, None) 
    for i in range(ocr_page.Zones.Count): 
        temp_zone = ocr_page.Zones[i] 
        temp_zone.ZoneType = OcrZoneType.Icr 
        ocr_page.Zones[i] = temp_zone 
    ocr_document.Save(r"C:\LEADTOOLS22\Resources\Images\icr.pdf", DocumentFormat.Pdf, None) 

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 auto-generates zones with AutoZone for an image to be used in ICR, and outputs the recognized image to a searchable PDF.


This tutorial showed how to run ICR on an image and output it to a searchable PDF. We also covered how to use the IOcrEngine and IOcrPage interfaces, along with the OcrZone structure.

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.