9951 explained code solutions for 126 technologies


tesseract-ocrHow do I use the Tesseract OCR engine to recognize text in an image?


The Tesseract OCR engine is an open source library for recognizing text in images. To use it, you need to install the Tesseract OCR library, which is available for Windows, Mac, and Linux.

Once you have installed the Tesseract library, you can use the following example code to recognize text in an image:

# Import the Tesseract OCR library
from tesserocr import PyTessBaseAPI

# Initialize the Tesseract OCR engine
api = PyTessBaseAPI()

# Open the image
image = Image.open('image.png')

# Recognize the text in the image
api.SetImage(image)
text = api.GetUTF8Text()

# Print the recognized text
print(text)

This code will open the image file image.png and recognize any text in the image. The recognized text will then be printed out.

The code can be broken down as follows:

  • from tesserocr import PyTessBaseAPI: imports the Tesseract OCR library.
  • api = PyTessBaseAPI(): initializes the Tesseract OCR engine.
  • image = Image.open('image.png'): opens the image file image.png.
  • api.SetImage(image): sets the image to be recognized.
  • text = api.GetUTF8Text(): recognizes the text in the image.
  • print(text): prints out the recognized text.

For more information about using the Tesseract OCR engine, please refer to the official documentation.

Edit this code on GitHub