Preprocess an Image for OCR - Python

This tutorial shows how to preprocess an image for OCR in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to preprocess an image for OCR using the AutoPreprocess method in a Python Console application.
Completion Time 10 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 Preprocess an Image for OCR - 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:

Add the OCR Image Preprocessing Code

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 * 

Add a new method to the Project-Name.py file named ocr_preprocessing(). Call the ocr_preprocessing() 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:/LEADTOOLS23/Support/Common/License")) 
 
    ocr_preprocessing() 

Add the code below to the ocr_preprocessing() method to initialize the IOcrEngine, preprocess the loaded image, and run OCR exporting to a searchable PDF.

def ocr_preprocessing(): 
 
    tif_filename = r"C:\LEADTOOLS23\Resources\Images\Clean.tif" 
    pdf_filename = r"C:\LEADTOOLS23\Resources\Images\Clean.pdf" 
 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
 
    # Start the engine using default parameters 
    ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime") 
 
    # Create an OCR document 
    ocr_document = ocr_engine.DocumentManager.CreateDocument() 
 
    # Add the image to the document 
    ocr_page = ocr_document.Pages.AddPage(tif_filename, None) 
 
    # Auto-preprocess the image 
    ocr_page.AutoPreprocess(OcrAutoPreprocessPageCommand.Deskew, None) 
    ocr_page.AutoPreprocess(OcrAutoPreprocessPageCommand.Invert, None) 
    ocr_page.AutoPreprocess(OcrAutoPreprocessPageCommand.Rotate, None) 
 
    # Recognize it and save as PDF 
    ocr_page.Recognize(None) 
    ocr_document.Save(pdf_filename, 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 will "clean-up" the loaded image and export it as a searchable PDF.

Wrap-up

This tutorial showed how to preprocess an image for OCR using the AutoPreprocess method inside the IOcrPage interface, and save it as a searchable PDF. Also, we covered how to use the IOcrDocument and IOcrEngine interfaces.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.