pytesseract

A Python wrapper for Google Tesseract

https://github.com/madmaze/pytesseract

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.3%) to scientific vocabulary

Keywords from Contributors

pillow pil asyncio asgi distributed opencv requests redoc pydantic pypi
Last synced: 10 months ago · JSON representation

Repository

A Python wrapper for Google Tesseract

Basic Info
  • Host: GitHub
  • Owner: madmaze
  • License: apache-2.0
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 1.48 MB
Statistics
  • Stars: 6,189
  • Watchers: 106
  • Forks: 735
  • Open Issues: 15
  • Releases: 26
Created over 15 years ago · Last pushed 11 months ago
Metadata Files
Readme License

README.rst

Python Tesseract
================

.. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg
   :target: https://pypi.python.org/pypi/pytesseract
   :alt: Python versions

.. image:: 	https://img.shields.io/github/release/madmaze/pytesseract.svg
   :target: https://github.com/madmaze/pytesseract/releases
   :alt: Github release

.. image:: https://img.shields.io/pypi/v/pytesseract.svg?color=blue
   :target: https://pypi.python.org/pypi/pytesseract
   :alt: PyPI release

.. image:: https://img.shields.io/conda/vn/conda-forge/pytesseract.svg?color=blue
   :target: https://anaconda.org/conda-forge/pytesseract
   :alt: Conda release

.. image:: https://results.pre-commit.ci/badge/github/madmaze/pytesseract/master.svg
   :target: https://results.pre-commit.ci/latest/github/madmaze/pytesseract/master
   :alt: Pre-commit CI status

.. image:: https://github.com/madmaze/pytesseract/workflows/CI/badge.svg?branch=master
    :target: https://github.com/madmaze/pytesseract/actions?query=workflow%3ACI
    :alt: CI workflow status

Python-tesseract is an optical character recognition (OCR) tool for python.
That is, it will recognize and "read" the text embedded in images.

Python-tesseract is a wrapper for `Google's Tesseract-OCR Engine `_.
It is also useful as a stand-alone invocation script to tesseract, as it can read all image types
supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, bmp, tiff,
and others. Additionally, if used as a script, Python-tesseract will print the recognized
text instead of writing it to a file.

USAGE
-----

**Quickstart**

*Note*: Test images are located in the ``tests/data`` folder of the Git repo.

Library usage:

.. code-block:: python

    from PIL import Image

    import pytesseract

    # If you don't have tesseract executable in your PATH, include the following:
    pytesseract.pytesseract.tesseract_cmd = r''
    # Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'

    # Simple image to string
    print(pytesseract.image_to_string(Image.open('test.png')))

    # In order to bypass the image conversions of pytesseract, just use relative or absolute image path
    # NOTE: In this case you should provide tesseract supported images or tesseract will return error
    print(pytesseract.image_to_string('test.png'))

    # List of available languages
    print(pytesseract.get_languages(config=''))

    # French text image to string
    print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))

    # Batch processing with a single file containing the list of multiple image file paths
    print(pytesseract.image_to_string('images.txt'))

    # Timeout/terminate the tesseract job after a period of time
    try:
        print(pytesseract.image_to_string('test.jpg', timeout=2)) # Timeout after 2 seconds
        print(pytesseract.image_to_string('test.jpg', timeout=0.5)) # Timeout after half a second
    except RuntimeError as timeout_error:
        # Tesseract processing is terminated
        pass

    # Get bounding box estimates
    print(pytesseract.image_to_boxes(Image.open('test.png')))

    # Get verbose data including boxes, confidences, line and page numbers
    print(pytesseract.image_to_data(Image.open('test.png')))

    # Get information about orientation and script detection
    print(pytesseract.image_to_osd(Image.open('test.png')))

    # Get a searchable PDF
    pdf = pytesseract.image_to_pdf_or_hocr('test.png', extension='pdf')
    with open('test.pdf', 'w+b') as f:
        f.write(pdf) # pdf type is bytes by default

    # Get HOCR output
    hocr = pytesseract.image_to_pdf_or_hocr('test.png', extension='hocr')

    # Get ALTO XML output
    xml = pytesseract.image_to_alto_xml('test.png')

    # getting multiple types of output with one call to save compute time
    # currently supports mix and match of the following: txt, pdf, hocr, box, tsv
    text, boxes = pytesseract.run_and_get_multiple_output('test.png', extensions=['txt', 'box'])

Support for OpenCV image/NumPy array objects

.. code-block:: python

    import cv2

    img_cv = cv2.imread(r'//digits.png')

    # By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
    # we need to convert from BGR to RGB format/mode:
    img_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
    print(pytesseract.image_to_string(img_rgb))
    # OR
    img_rgb = Image.frombytes('RGB', img_cv.shape[:2], img_cv, 'raw', 'BGR', 0, 0)
    print(pytesseract.image_to_string(img_rgb))


If you need custom configuration like `oem`/`psm`, use the **config** keyword.

.. code-block:: python

    # Example of adding any additional options
    custom_oem_psm_config = r'--oem 3 --psm 6'
    pytesseract.image_to_string(image, config=custom_oem_psm_config)

    # Example of using pre-defined tesseract config file with options
    cfg_filename = 'words'
    pytesseract.run_and_get_output(image, extension='txt', config=cfg_filename)

Add the following config, if you have tessdata error like: "Error opening data file..."

.. code-block:: python

    # Example config: r'--tessdata-dir "C:\Program Files (x86)\Tesseract-OCR\tessdata"'
    # It's important to add double quotes around the dir path.
    tessdata_dir_config = r'--tessdata-dir ""'
    pytesseract.image_to_string(image, lang='chi_sim', config=tessdata_dir_config)

**Functions**

* **get_languages** Returns all currently supported languages by Tesseract OCR.

* **get_tesseract_version** Returns the Tesseract version installed in the system.

* **image_to_string** Returns unmodified output as string from Tesseract OCR processing

* **image_to_boxes** Returns result containing recognized characters and their box boundaries

* **image_to_data** Returns result containing box boundaries, confidences, and other information. Requires Tesseract 3.05+. For more information, please check the `Tesseract TSV documentation `_

* **image_to_osd** Returns result containing information about orientation and script detection.

* **image_to_alto_xml** Returns result in the form of Tesseract's ALTO XML format.

* **run_and_get_output** Returns the raw output from Tesseract OCR. Gives a bit more control over the parameters that are sent to tesseract.

* **run_and_get_multiple_output** Returns like `run_and_get_output` but can handle multiple extensions. This function replaces the `extension: str` kwarg with `extension: List[str]` kwarg where a list of extensions can be specified and the corresponding data is returned after only one `tesseract` call. This function reduces the number of calls to `tesseract` when multiple output formats, like both text and bounding boxes,  are needed.

**Parameters**

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

* **image** Object or String - either PIL Image, NumPy array or file path of the image to be processed by Tesseract. If you pass object instead of file path, pytesseract will implicitly convert the image to `RGB mode `_.

* **lang** String - Tesseract language code string. Defaults to ``eng`` if not specified! Example for multiple languages: ``lang='eng+fra'``

* **config** String - Any **additional custom configuration flags** that are not available via the pytesseract function. For example: ``config='--psm 6'``

* **nice** Integer - modifies the processor priority for the Tesseract run. Not supported on Windows. Nice adjusts the niceness of unix-like processes.

* **output_type** Class attribute - specifies the type of the output, defaults to ``string``.  For the full list of all supported types, please check the definition of `pytesseract.Output `_ class.

* **timeout** Integer or Float - duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError.

* **pandas_config** Dict - only for the **Output.DATAFRAME** type. Dictionary with custom arguments for `pandas.read_csv `_. Allows you to customize the output of **image_to_data**.

CLI usage:

.. code-block:: bash

    pytesseract [-l lang] image_file

INSTALLATION
------------

Prerequisites:

- Python-tesseract requires Python 3.6+
- You will need the Python Imaging Library (PIL) (or the `Pillow `_ fork).
  Please check the `Pillow documentation `_ to know the basic Pillow installation.
- Install `Google Tesseract OCR `_
  (additional info how to install the engine on Linux, Mac OSX and Windows).
  You must be able to invoke the tesseract command as *tesseract*. If this
  isn't the case, for example because tesseract isn't in your PATH, you will
  have to change the "tesseract_cmd" variable ``pytesseract.pytesseract.tesseract_cmd``.
  Under Debian/Ubuntu you can use the package **tesseract-ocr**.
  For Mac OS users. please install homebrew package **tesseract**.

  *Note:* In some rare cases, you might need to additionally install ``tessconfigs`` and ``configs`` from `tesseract-ocr/tessconfigs `_ if the OS specific package doesn't include them.

| Installing via pip:

Check the `pytesseract package page `_ for more information.

.. code-block:: bash

    pip install pytesseract

| Or if you have git installed:

.. code-block:: bash

    pip install -U git+https://github.com/madmaze/pytesseract.git

| Installing from source:

.. code-block:: bash

    git clone https://github.com/madmaze/pytesseract.git
    cd pytesseract && pip install -U .

| Install with conda (via `conda-forge `_):

.. code-block:: bash

    conda install -c conda-forge pytesseract

TESTING
-------

To run this project's test suite, install and run ``tox``. Ensure that you have ``tesseract``
installed and in your PATH.

.. code-block:: bash

    pip install tox
    tox

LICENSE
-------
Check the LICENSE file included in the Python-tesseract repository/distribution.
As of Python-tesseract 0.3.1 the license is Apache License Version 2.0

CONTRIBUTORS
------------
- Originally written by `Samuel Hoffstaetter `_
- `Full list of contributors `_

Owner

  • Name: Matthias A Lee
  • Login: madmaze
  • Kind: user

Principal Software Engineer, Computer Science PhD. Interest span eHealth, NoSQL, cloud computing, GPUs and Software Performance

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 507
  • Total Committers: 47
  • Avg Commits per committer: 10.787
  • Development Distribution Score (DDS): 0.4
Past Year
  • Commits: 11
  • Committers: 3
  • Avg Commits per committer: 3.667
  • Development Distribution Score (DDS): 0.273
Top Committers
Name Email Commits
int3l i****l 304
pre-commit-ci[bot] 6****] 103
Samuel Hoffstaetter s****l@h****m 16
johnthagen j****n 12
madmaze m****e@g****m 12
Yao You y****o@u****o 8
Bozho Dimitrov 1****v 3
Emilio Cecchini c****r@g****m 3
REMitchell R****l@g****m 3
plokmijnuhby 3****y 3
Sr4l L****r@s****e 2
ELToulemonde e****e@p****m 2
Darius Morawiec n****k 2
Alexey Stavrov l****z@y****u 1
Benedikt S. Vogler m****l@b****m 1
CarpyCar c****r@g****m 1
Christian Hogan 1****3@g****m 1
Emmanuel e****y@m****m 1
Evstrat_BG e****g@m****u 1
2press p****e@y****m 1
ugurcanakyuz u****z@o****m 1
knicki m****t@g****e 1
Simeon Visser s****n@r****m 1
Josh Reid j****3@g****m 1
lasfter l****r@g****m 1
jbochi j****i@g****m 1
igor-ma 4****a 1
eivtho 5****o 1
egeakman me@e****v 1
diditforlulz273 5****3 1
and 17 more...

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 101
  • Total pull requests: 91
  • Average time to close issues: 5 months
  • Average time to close pull requests: 16 days
  • Total issue authors: 90
  • Total pull request authors: 17
  • Average comments per issue: 3.6
  • Average comments per pull request: 0.49
  • Merged pull requests: 79
  • Bot issues: 0
  • Bot pull requests: 68
Past Year
  • Issues: 9
  • Pull requests: 14
  • Average time to close issues: 30 days
  • Average time to close pull requests: 4 days
  • Issue authors: 8
  • Pull request authors: 3
  • Average comments per issue: 1.78
  • Average comments per pull request: 0.0
  • Merged pull requests: 7
  • Bot issues: 0
  • Bot pull requests: 11
Top Authors
Issue Authors
  • Dineshkumar-Anandan-ZS0367 (3)
  • stefan6419846 (3)
  • caerulescens (2)
  • YashMistry349 (2)
  • oditorium (2)
  • int3l (2)
  • Rohinivv96 (2)
  • KaipaUday (2)
  • me-suzy (2)
  • ELToulemonde (2)
  • zwl1619 (1)
  • agutkarsh (1)
  • Sammeeey (1)
  • daniel-xion (1)
  • larawehbe (1)
Pull Request Authors
  • pre-commit-ci[bot] (79)
  • int3l (4)
  • egeakman (3)
  • badGarnet (2)
  • mausam3407 (2)
  • AdventurousGui (2)
  • KaipaUday (2)
  • kuroxx (1)
  • peter279k (1)
  • PatrickDaG (1)
  • plokmijnuhby (1)
  • ElyesDer (1)
  • caerulescens (1)
  • marosstruk (1)
  • guillochon (1)
Top Labels
Issue Labels
Feature Request (9) Todo (3) Bug (1)
Pull Request Labels

Packages

  • Total packages: 6
  • Total downloads:
    • pypi 5,265,531 last-month
  • Total docker downloads: 1,408,538
  • Total dependent packages: 359
    (may contain duplicates)
  • Total dependent repositories: 4,576
    (may contain duplicates)
  • Total versions: 78
  • Total maintainers: 3
pypi.org: pytesseract

Python-tesseract is a python wrapper for Google's Tesseract-OCR

  • Versions: 28
  • Dependent Packages: 352
  • Dependent Repositories: 4,542
  • Downloads: 4,846,266 Last month
  • Docker Downloads: 1,408,538
Rankings
Dependent packages count: 0.1%
Dependent repos count: 0.1%
Downloads: 0.3%
Stargazers count: 0.4%
Average: 0.6%
Docker downloads count: 0.8%
Forks count: 1.7%
Maintainers (1)
Last synced: 10 months ago
pypi.org: unstructured.pytesseract

Python-tesseract is a python wrapper for Google's Tesseract-OCR

  • Versions: 4
  • Dependent Packages: 3
  • Dependent Repositories: 4
  • Downloads: 419,265 Last month
Rankings
Stargazers count: 0.4%
Downloads: 0.8%
Forks count: 1.7%
Average: 2.7%
Dependent packages count: 3.2%
Dependent repos count: 7.5%
Maintainers (1)
Last synced: 10 months ago
proxy.golang.org: github.com/madmaze/pytesseract
  • Versions: 30
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 0.9%
Forks count: 1.1%
Average: 5.6%
Dependent packages count: 9.6%
Dependent repos count: 10.8%
Last synced: 11 months ago
conda-forge.org: pytesseract

Python-tesseract is a wrapper for Google's Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, bmp, tiff, and others. Additionally, if used as a script, Python-tesseract will print the recognized text instead of writing it to a file

  • Versions: 14
  • Dependent Packages: 4
  • Dependent Repositories: 15
Rankings
Stargazers count: 5.0%
Forks count: 6.3%
Average: 8.3%
Dependent repos count: 9.2%
Dependent packages count: 12.5%
Last synced: 11 months ago
spack.io: py-pytesseract

Python-tesseract is an Optical Character Recognition (OCR) Tool for python.

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 0.0%
Stargazers count: 2.3%
Forks count: 3.6%
Average: 15.8%
Dependent packages count: 57.3%
Maintainers (1)
Last synced: 11 months ago
anaconda.org: pytesseract

Python-tesseract is a wrapper for Google's Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, bmp, tiff, and others. Additionally, if used as a script, Python-tesseract will print the recognized text instead of writing it to a file

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 15
Rankings
Stargazers count: 11.7%
Forks count: 13.5%
Average: 27.8%
Dependent repos count: 34.7%
Dependent packages count: 51.2%
Last synced: 11 months ago

Dependencies

.github/workflows/ci.yaml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
requirements-dev.txt pypi
  • pre-commit * development
  • pytest * development