Hello, world!

In this article, you will learn how to build a bare minimum Python console application for extracting text from an image with Aspose.OCR for Python via .NET.

You will need

  • A compatible system with Python 3.6 or later installed. It is recommended to use the latest version of Python.
  • Pip package installer.
  • Any plain text editor.
  • Some image containing a text. You can simply download the one from the article.
  • 5 minutes of spare time.

Preparing the environment

  1. Install Aspose.OCR for Python via .NET package using pip.
  2. Create a text file called ocr.py.
  3. Save this image file to the same directory as ocr.py under the name source.png:
    Source image

Coding

Open ocr.py in whatever code or plain text editor you prefer.

  1. Import aspose.ocr module:
    import aspose.ocr as ocr
    
  2. Create an instance of Aspose.OCR API:
    api = AsposeOcr()
    
  3. Add the image to the recognition batch:
    input = OcrInput(InputType.SINGLE_IMAGE)
    input.add("source.png")
    
  4. Extract text from the image:
    result = api.recognize(input)
    
  5. Output the recognized text:
    print(result[0].recognition_text)
    

Full code:

import aspose.ocr as ocr

# Instantiate Aspose.OCR API
api = AsposeOcr()

# Add image to the recognition batch
input = OcrInput(InputType.SINGLE_IMAGE)
input.add("source.png")

# Recognize the image
result = api.recognize(input)

# Print recognition result
print(result[0].recognition_text)
input("Press Enter to continue...")

Running

Run ocr.py. You will see extracted text in the console output:

Hello, World! I can read this text.
Press Enter to continue...