Blog
Pytesseract: how to convert images to text with Python [tutorial]
In short
Pytesseract makes Tesseract OCR usable from Python: image in, text out. The guide below goes from installation to the first result. If you need fields and line items from invoices or orders rather than raw text, the end of the article shows where Tesseract stops.
- Published on 1 September 2023
- Updated on 26 January 2024
Pytesseract is a Python library built on Google's Tesseract OCR that makes optical character recognition available in Python applications. This article shows how to install, configure and use Pytesseract to recognise text in images.
What is Pytesseract?
Pytesseract is a Python library that uses Tesseract OCR to extract text from images. Tesseract OCR is open-source software developed by Google for optical character recognition (OCR): it recognises text in image files and converts it into editable text. Pytesseract provides a simple interface for using Tesseract from Python.
Reading text from an image with Pytesseract, step by step
Reading text from an image with Pytesseract is fairly simple. These are the steps:
Install Tesseract on Windows
Tesseract OCR was originally developed for Linux. It is available for Windows thanks to the University of Mannheim, which uses Tesseract to process historical German newspapers. You can download the Windows installer for Tesseract from GitHub: https://github.com/UB-Mannheim/tesseract/wiki
This installer sets up Tesseract OCR on Windows so that Pytesseract can use it.
Note: if the German language pack was not included when you installed Tesseract, you need to install it separately. To do this, download the training data for German and copy it into the ‘tessdata’ directory, which is located at, for example, “C:\Program Files (x86)\Tesseract-OCR\tessdata”. Training data for many languages, including German, is available here: https://github.com/tesseract-ocr/tessdata/blob/main/deu.traineddata
Install Pytesseract
Once Tesseract is installed on your operating system, install the modules “Image” and “pytesseract” with the pip package manager.
pip install pytesseract pillow
Import the modules
Import the required modules in your Python code.
from PIL import Image
import pytesseract
Set the path to Tesseract OCR
Specify the path to your Tesseract OCR installation so that Pytesseract can find the executable.
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
Load the image
Load the image you want to extract text from using the Python Imaging Library (PIL).
image = Image.open("scan.png")
Run text recognition
Use Pytesseract's image_to_string() method to extract the text from the image:
text = pytesseract.image_to_string(image, lang="eng")
print(text)
Example output
When Tesseract is not enough
Tesseract gives you text, not meaning. For an invoice or a purchase order that means you get a string and have to work out yourself which number is the total, where the table starts and whether the item number exists at all. Typical limits:
- Tesseract returns no fields. Invoice number, date or supplier have to be found with regexes or by position, again for every layout.
- Tables fall apart, and line items with quantity, unit and price come back as running text.
- Nothing checks the values. Only matching against your master data shows whether a value read is right.
This is where smartextract starts: fields and line items are read without a template per layout, checked against your master data and handed over as JSON via the API or exported as CSV, XLS or XML. Read the API documentation · Try it with 100 free credits
What Pytesseract does well
Pytesseract is the standard tool for optical character recognition in Python. It extracts text from images so you can reuse it as data.
If you can extract text from images, nobody has to type it up. With Pytesseract, developers can build their own applications on top of that text recognition.
Send us a document and watch the extraction.
30 minutes, your own documents, no preparation needed.