Find Text in Picture using Python

In this tutorial, you will learn how to find text in picture using Python. It has all the details to set the environment, a list of steps, and a sample code for the image text search using Python. You will learn to set flags for customizing the search operation in different types of images such as PNG, JPG, BMP, TIFF, etc.

Steps to Find Text in Image using Python

  1. Set the environment to use Aspose.OCR for Python via .NET for searching words in an image
  2. Create an instance of the Aspose OCR engine using the AsposeOcr class
  3. Configure the recognition settings with the required language
  4. Check if the required word is present in the image using the image_has_text() method
  5. Display the appropriate message according to the search result

These steps summarize how to search text from image using Python. Create an instance of the Aspose OCR engine, configure the recognition settings, and define the target word for searching. Finally, call the image_has_text() method with the input image name, target word, flag to ignore the case, and auto skew flag for searching the word.

Code for Text Finder in Image using Python

import aspose.ocr as api # Import the Aspose OCR library
from aspose.ocr import License # Import the License class from the Aspose OCR library
# Set the Aspose OCR license
license = License()
license.set_license("license.lic") # Load the license file to activate the full features of Aspose OCR
# Create an instance of the Aspose OCR engine
asposeOcr = api.AsposeOcr()
# Configure recognition settings
settings = api.RecognitionSettings() # Create a settings object for OCR recognition
settings.language = api.Language.ENG # Set the recognition language to English (ENG)
# Define the target word to search for in the image
targetWord = "Restaurant"
# Check if the target word is present in the image
if asposeOcr.image_has_text("Receipt1.png", targetWord, settings, True, False):
# If the word is found, print a success message
print("The image contains the word " + targetWord)
else:
# If the word is not found, print a failure message
print("The image does not contain the word " + targetWord)

This sample code exhibits the search for text in image using Python. You can set customizations such as using additional algorithms for small font recognition, a flag for recognizing a single line, setting the list of text areas, and a flag to search text in tables. Other parameters can also be set like selecting text language in the image, a list of blacklisted symbols, and a list of allowed symbols and characters.

This article has taught us the process of text search from image using Python. If you want to extract text from an invoice, refer to the article on Data extraction from invoices using Python.

 English