keepcalculator

06 Numbers and Powers

Python Text Extraction from Images: OCR Guide

Card No. 06·60403 · Updated · Standard formula, shown below

Python can extract text from images using optical character recognition (OCR): a library analyzes pixels and returns likely characters and words. For a straightforward local workflow, install Tesseract OCR and use the Python package pytesseract; for difficult images, preprocessing or a different OCR engine may improve results.

How image text extraction works

An image stores visual information, not editable text. OCR software detects text regions, identifies shapes that resemble characters, and combines them into words and lines. Depending on the engine and settings, results may include plain text, confidence estimates, or coordinates for detected words.

OCR is an estimate, not a guaranteed transcription. A clear, straight scan of printed text is usually easier than handwriting, a curved page, a low-resolution screenshot, or text over a patterned background. Languages, fonts, page layout, and image quality all influence accuracy.

Python commonly serves as the connector: it opens and optionally prepares an image, sends it to an OCR engine, then uses the returned text in a larger task such as searching documents or extracting fields. The OCR engine does the character recognition; pytesseract provides a Python interface to Tesseract.

Install Tesseract and pytesseract

These are two separate components. Installing the Python package does not, by itself, install the Tesseract executable. Install Tesseract using the instructions for your operating system, then install the Python dependencies:

python -m pip install pytesseract pillow

Pillow is an image library that pytesseract can use to open common image files. If Python cannot find the Tesseract executable, configure its path in your script. The exact path depends on the operating system and installation location.

For example, on Windows you may need to set the executable path before calling OCR:

import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\path\to\tesseract.exe"

Replace the example with the real location on your computer. On many Linux and macOS setups, Tesseract is available through the system path after installation, so this setting is unnecessary.

Extract text from an image

For a basic image, open it with Pillow and pass it to image_to_string:

from PIL import Image
import pytesseract

image = Image.open("page.png")
text = pytesseract.image_to_string(image, lang="eng")
print(text)

The language code tells Tesseract which trained language data to use. The English language data is often installed by default, but this depends on how Tesseract was installed. For another language, install its trained data and use the corresponding code, such as fra for French where that data is available.

To read a PDF, first convert each page to an image with a PDF-rendering tool, then run OCR on those images. Some PDFs already contain a text layer; in that case, extracting the existing text directly is generally more reliable and faster than converting pages to images and recognizing them again.

Improve accuracy with preprocessing

Before changing OCR settings, inspect the source. Recognition often improves when text is large, sharply focused, evenly lit, and oriented horizontally. Pillow can resize, convert to grayscale, or adjust contrast, while OpenCV offers additional image-processing operations.

  • Correct rotation: Rotate a sideways or tilted page so its lines run horizontally. A small skew can affect recognition across a full page.
  • Increase useful resolution: If the source is too small, enlarge it carefully. Upscaling cannot restore details that were never captured, but may help some OCR workflows.
  • Reduce distracting color: Grayscale conversion can simplify an image, especially when the text and background differ mainly in brightness.
  • Test thresholding: Turning a grayscale image into black and white can make faint text clearer, but an unsuitable threshold can erase thin strokes or merge characters.
  • Crop to the relevant area: Removing borders and unrelated content can reduce confusion, especially for forms or screenshots.

Do not assume every transformation helps. Keep the original image and compare recognition results. A useful workflow is to try one change at a time, then check representative pages rather than judging only a single easy example.

Choose the right OCR approach

Tesseract with pytesseract is a practical starting point for printed text and local processing. It can be used without sending images to a hosted service, although the privacy of a complete workflow also depends on where files are stored and how the application handles them.

Other options include OCR libraries that bundle recognition models and hosted OCR services. They differ in language support, layout analysis, handwriting recognition, deployment requirements, and whether images leave your environment. Compare them using images like the ones your application will actually process. A result that works well on clean pages may fail on receipts, tables, or handwritten notes.

If you need word locations rather than a single text string, pytesseract offers functions such as image_to_data, which returns recognized text alongside information such as bounding boxes and confidence values. Those values help identify uncertain regions, but they are not proof that a word is correct. For structured documents, OCR may identify the text without understanding which value belongs in which field.

Common problems and limitations

  • “Tesseract is not installed or not in PATH”: Confirm that the executable is installed and accessible. If necessary, set tesseract_cmd to its actual path.
  • Language data errors: Check that the requested trained language data is installed and that the language code is spelled correctly. Multiple languages can be requested when their data is present.
  • Empty or garbled output: Check that the image opens correctly, contains legible text, and is not rotated or excessively compressed. Try a crop or a different preprocessing step.
  • Incorrect reading order: Multi-column pages, tables, and mixed text-and-image layouts can be difficult. OCR may read across columns or omit relationships between labels and values.

OCR can also confuse visually similar characters, such as O and 0, or l and 1. This matters for names, identifiers, measurements, and other data where a single character changes the meaning. Validate extracted content against the source when accuracy matters; do not treat OCR output as authoritative without review.

Frequently Asked Questions

Can Python extract text from a JPG or PNG?
Yes. With Tesseract installed, pytesseract and Pillow can open common image formats such as JPG and PNG and return recognized text.

Does pytesseract install Tesseract?
No. Pytesseract is a Python wrapper. Install the Tesseract OCR program separately, then make sure Python can locate its executable.

Can OCR read handwriting?
It may recognize some handwriting, but results are generally less dependable than for clear printed text. Test an OCR system on the handwriting style and image conditions you need to support.

How do I extract text from a scanned PDF?
Render the PDF pages as images, then run OCR on each page. First check whether the PDF already has a usable text layer, since direct text extraction avoids unnecessary recognition.

How can I improve OCR accuracy in Python?
Start with a sharp, straight image and the correct language data. Then test cropping, resizing, grayscale conversion, or thresholding, checking the output against the original.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →