pytesseract Documentation

repository·master·Indexed 27 days ago

https://github.com/madmaze/pytesseract

A Python wrapper for Google's Tesseract-OCR Engine used to extract text, bounding boxes, and metadata from images. It provides functions such as image_to_string, image_to_boxes, and image_to_data, and supports output formats including PDF, HOCR, and ALTO XML. Requires Python 3.6+, Pillow, and the Tesseract OCR engine.

Tokens
1K
Snippets
2
Records
10
Agent score
42%

What's inside pytesseract

  1. Process OpenCV/NumPy images

    master

    Since OpenCV loads images in BGR format and pytesseract expects RGB, you must convert the color space before processing.

    import cv2
    import pytesseract
    from PIL import Image
    
    img_cv = cv2.imread('digits.png')
    
    # Option 1: Convert BGR to RGB using OpenCV
    img_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
    print(pytesseract.image_to_string(img_rgb))
    
    # Option 2: Convert using Pillow
    img_rgb = Image.frombytes('RGB', img_cv.shape[:2], img_cv, 'raw', 'BGR', 0, 0)
    print(pytesseract.image_to_string(img_rgb))
  2. Install pytesseract

    master

    You can install pytesseract via pip, conda, or from source.

    Prerequisites:

    • Python 3.6+
    • Pillow (Python Imaging Library)
    • Google Tesseract OCR engine installed on your system. You must be able to invoke the tesseract command from your terminal. If it is not in your PATH, you must configure pytesseract.pytesseract.tesseract_cmd in your code.
  3. Use custom Tesseract configuration flags

    master
    Pass additional Tesseract configuration flags (like oem or psm) using the config keyword argument. This is also the method to resolve tessdata directory errors by specifying the --tessdata-dir path.
  4. Perform simple image to string OCR

    master
    Use image_to_string to extract text from an image. You can pass a PIL Image object or a string representing the file path. If you pass a file path, pytesseract bypasses internal image conversions, but you must ensure the file is a format supported by Tesseract.
  5. Generate PDF, HOCR, or ALTO XML output

    master

    You can export OCR results into specific formats using image_to_pdf_or_hocr or image_to_alto_xml.

    # Get a searchable PDF (returns bytes)
    pdf = pytesseract.image_to_pdf_or_hocr('test.png', extension='pdf')
    with open('test.pdf', 'w+b') as f:
        f.write(pdf)
    
    # Get HOCR output
    hocr = pytesseract.image_to_pdf_or_hocr('test.png', extension='hocr')
    
    # Get ALTO XML output
    alto_xml = pytesseract.image_to_alto_xml('test.png')
  6. Extract bounding boxes and detailed data

    master

    Use specialized functions to get more than just raw text:

    • image_to_boxes: Returns recognized characters and their box boundaries.
    • image_to_data: Returns verbose data including boxes, confidences, line, and page numbers. Requires Tesseract 3.05+.
    • image_to_osd: Returns information about orientation and script detection.
  7. Reference: image_to_data parameters

    master

    The image_to_data function signature and parameters:

    image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0, pandas_config=None)

    • image: PIL Image, NumPy array, or file path string.
    • lang: Tesseract language code (e.g., 'eng', 'fra', or 'eng+fra'). Defaults to 'eng'.
    • config: String of additional custom configuration flags.
    • nice: Integer. Modifies processor priority (Unix-like only; not supported on Windows).
    • output_type: Specifies the output type (defaults to string). See pytesseract.Output for all types.
    • timeout: Integer or Float. Seconds after which the job is terminated via RuntimeError.
    • pandas_config: Dict. Custom arguments for pandas.read_csv (only used when output_type is Output.DATAFRAME).