Detect and Extract MRTD - Python

This tutorial shows how to detect and extract passport information from a given image in a Python application using the LEADTOOLS SDK.

Summary This tutorial covers how to find and extract passport features from 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 and loading an image by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on the Detect and Extract MRTD - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Load and Save Images 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 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 MRTD Reader and Processing Code

With the project created, the references added, the license set, and the load image code added, coding can begin. The image save code is not necessary for this tutorial, so that code can be commented out or deleted.

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.Codecs import * 
from Leadtools.Forms.Commands import * 
from Leadtools.Ocr import * 
from System.IO import *  

Add a new method to the file named extract_mrtd(image). Call this method below the call to the load_image() method inside the main() method, as shown below.

Ensure that you replace the string value passed into the load_image() method with the file path to your passport image. For the purposes of this tutorial the following image is used: C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg

image = load_image(r"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg") 

Add the code below to the extract_mrtd() method to process the given image and output the MRTD values if detected.

def extract_mrtd(image): 
    mrtd_reader = MRTDReader() 
    # Create the OCR Engine 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime") 
    mrtd_reader.OcrEngine = ocr_engine 
    # Process Image 
    # Output values 
    if (mrtd_reader.Errors == MRTDErrors.NoError): 
        for value in mrtd_reader.Results: 
            print(f"Data Element Field: {value.Key.ToString()}") 
            print(f"Data Element Value: {value.Value.ReadableValue}") 
            print(f"Data Element Code: {value.Value.MrzCharacters}") 
            print(f"Data Element Valid: {value.Value.IsValid}") 

Handling Streams

To handle files using memory stream, then insert the following code into the main() method:

filename = r"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg" 
buffer = File.ReadAllBytes(filename) 
codecs = RasterCodecs() 
ms = MemoryStream(buffer) 
ms.Position = 0 
image = codecs.Load(ms) 
# image = load_image(r"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg") 
# extract_mrtd(image) 


This code will replace the existing code in the main() method under the Support.set_license() call.

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 detects the MRTD text and displays the information to the console.

Extracted MRTD information displayed to the console.


This tutorial showed how to load an image and run passport recognition using the MRTDReader class.

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.