This tutorial shows how to set up the LEAD OCR Engine with minimal code to process OCR in a Python Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to set up the LEAD OCR Engine 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 |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Minimum Code Required to OCR Files - Python tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, 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.Document.Writer.dll
Leadtools.Ocr.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.Document.Writer")
from Leadtools.Document.Writer import *
LibraryLoader.add_reference("Leadtools.Ocr")
from Leadtools.Ocr import *
from System.IO import *
Add a new method named ocr(input_file, output_file)
to the Project-Name.py
file and call it inside the main()
method below Support.set_license()
.
def main():
Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License"))
input_file = r"C:\LEADTOOLS23\Resources\Images\OCR1.TIF"
output_file = r"C:\LEADTOOLS23\Resources\Images\OCR1.PDF"
ocr(input_file, output_file)
Add the code below to the new method, to initialize the IOcrEngine
, run OCR on the input image, and export the file to searchable PDF.
def ocr(input_file, output_file):
ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)
ocr_engine.Startup(None, None, None, "C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime")
ocr_engine.AutoRecognizeManager.Run(input_file, output_file, DocumentFormat.Pdf, None, None)
print(f"OCR output saved to {output_file}")
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application converts the input image to PDF format and saves it to the specified location (as a searchable PDF).
This tutorial showed how to create a simple OCR Python Application that initializes the LEAD OCR Engine, takes a specified input file and outputs the recognition results to the specified output file in the specified format with minimal code.