Hello, world!
Contents
[
Hide
]
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.
We assume that you already have a basic knowledge of how to write and run Python scripts.
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
- Install Aspose.OCR for Python via .NET package using pip.
- Create a text file called
ocr.py
. - Save this image file to the same directory as ocr.py under the name source.png:
Coding
Open ocr.py in whatever code or plain text editor you prefer.
- Import aspose.ocr module:
import aspose.ocr as ocr
- Create an instance of Aspose.OCR API:
api = AsposeOcr()
- Add the image to the recognition batch:
input = OcrInput(InputType.SINGLE_IMAGE) input.add("source.png")
- Extract text from the image:
result = api.recognize(input)
- 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...
If you use your own image, consider the trial restrictions.