Skip to main content

Pysseract

Build Status

A Python binding to Tesseract API. Tesseract is an open-source tool made available by Google for Optical Character Recognition (OCR) - that is, getting a computer to read the text in an image. Tesseract allows you to perform this task at a number of levels of granularity (one character at a time, one word at a time, and so on), by segmenting the page in a number of different ways (by assuming the whole page is one lump of text, or one line, or sparsely located throughout the source image), and with a number of different language models including ones you have built (pre-built models are available at https://github.com/tesseract-ocr/tessdata among other places).

Pip 19.3.1 or greater is required if you're installing the wheel for this package, otherwise just install the source. On Linux, if you install the wheel Tesseract comes included. You will however need to provide the Tesseract models. An example of how you might do this with English on a linux system is as follows:

curl -O https://raw.githubusercontent.com/tesseract-ocr/tessdata_fast/4.0.0/eng.traineddata
mkdir -p /usr/local/share/tessdata/ && sudo mv eng.traineddata /usr/local/share/tessdata/ 

The reason the file is being put in to /usr/local/share/tessdata/ is because that is the default value for TESSDATA_PREFIX, an environment variable that Tesseract uses to locate model files. You're free to override the value of TESSDATA_PREFIX, of course.

Documentation is hosted on readthedocs.

Basic usage

In order to just get all the text from an image and concatenate it into a string, run the following:

import pysseract
t = pysseract.Pysseract()
t.SetImageFromPath('tests/001-helloworld.png')
print(t.utf8Text)

If instead you want to iterate through the text boxes found in an image at the TEXTLINE level (coarser-grained than WORD, but also lower-level than BLOCK), then you might run the following:

with pysseract.Pysseract() as t:
    boxes = []
    text = []
    conf = []
    LEVEL = pysseract.PageIteratorLevel.TEXTLINE
    for box, text, confidence in t.IterAt(LEVEL):
        lines.append(text)
        boxes.append(box)
        confidence.append(conf)

A third possibility is that you may want to control how exactly the image is segmented. This is done before instantiating a ResultIterator, as follows:

with pysseract.Pysseract() as t:
    t.pageSegMode = pysseract.PageSegMode.SINGLE_BLOCK
    t.SetImageFromPath("002-quick-fox.jpg")
    t.SetSourceResolution(70)
    boxes = []
    text = []
    conf = []
    LEVEL = pysseract.PageIteratorLevel.TEXTLINE
    for box, text, confidence in t.IterAt(LEVEL):
        lines.append(text)
        boxes.append(box)
        confidence.append(conf)

Finally, if you want to work with the low-level iterator built into Tesseract, the below code will work for you. This is primarily intended for people who want fine-grain control when searching through the results. For instance, if you want to look at the first paragraph, jump to the next word, then the next block after that, then the next symbol after that, you would use this approach:

t = pysseract.Pysseract()
t.SetImageFromPath("002-quick-fox.jpg")
resIter = t.GetIterator()
boxes = []
lines = []
confidence = []

# First, look at the paragraph level
level = pysseract.PageIteratorLevel.PARA
boxes.append(resIter.BoundingBox(level))
lines.append(resIter.GetUTF8Text(level))
confidence.append(resIter.Confidence(level))

# Now the next word after the paragraph we just looked at
level = pysseract.PageIteratorLevel.WORD
resIter.Next(level)
boxes.append(resIter.BoundingBox(level))
lines.append(resIter.GetUTF8Text(level))
confidence.append(resIter.Confidence(level))

# Now the next block
level = pysseract.PageIteratorLevel.BLOCK
resIter.Next(level)
boxes.append(resIter.BoundingBox(level))
lines.append(resIter.GetUTF8Text(level))
confidence.append(resIter.Confidence(level))

# Lastly, look at the next symbol after the block we just looked at
level = pysseract.PageIteratorLevel.SYMBOL
resIter.Next(level)
boxes.append(resIter.BoundingBox(level))
lines.append(resIter.GetUTF8Text(level))
confidence.append(resIter.Confidence(level))

Building the package

Requirements

  • gcc/clang with at least c++11 support
  • libtesseract, libtesseract-dev (equivalent on non-Debian/Ubuntu systems)
  • pybind11>=2.2
python3 setup.py build install test

Building the documentation

pip install sphinx sphinx_rtd_theme m2r
python3 setup.py build_sphinx

You should find the generated html in build/sphinx.

Contribute

Look at Tesseract BaseAPI and import those functions of interest to pymodule.cpp.

Please write a brief description in your wrapper function like those already in pymodule.cpp.

Reference

LICENSE

MIT

Release files for pysseract 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysseract 1.3.1
File Size Uploaded
pysseract-1.3.1.tar.gz 13.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pysseract 1.3.1
File
pysseract-1.3.1-cp38-cp38-manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details
pysseract-1.3.1-cp38-cp38-manylinux2014_i686.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-32 Details
pysseract-1.3.1-cp37-cp37m-manylinux2014_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64 Details
pysseract-1.3.1-cp37-cp37m-manylinux2014_i686.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-32 Details
pysseract-1.3.1-cp36-cp36m-manylinux2014_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-64 Details
pysseract-1.3.1-cp36-cp36m-manylinux2014_i686.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-32 Details
pysseract-1.3.1-cp35-cp35m-manylinux2014_x86_64.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.17+ x86-64 Details
pysseract-1.3.1-cp35-cp35m-manylinux2014_i686.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.17+ x86-32 Details

Total release size: 178.1 MB

Release files / pysseract-1.3.1.tar.gz

Download URL pysseract-1.3.1.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
416658ef9335f22b928b131a7392ddf073e20b9382f139fc42a50a4cca9eabfe
BLAKE2b-256 checksum
How to use checksums
175fd94d064e54254cef5bade2ab69168e2ebff6c6edfef8998ababaca01a667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp38-cp38-manylinux2014_x86_64.whl

Download URL pysseract-1.3.1-cp38-cp38-manylinux2014_x86_64.whl
Size 25.6 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
3d3265190084c0ff1b150b656c12c3e67316c3715bbc2062272fee35a2980a1c
BLAKE2b-256 checksum
How to use checksums
0bb0b14e6326590e3a6bd0ffe1b15d03f10cfdccd61cec7b65b7bd941bbe4483
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp38-cp38-manylinux2014_i686.whl

Download URL pysseract-1.3.1-cp38-cp38-manylinux2014_i686.whl
Size 18.9 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
0c8a974c2bbf79cfd56bdef50526aa8d2e35214e2863a14c636f58eaeb2433e3
BLAKE2b-256 checksum
How to use checksums
baa4950b098c94ffb1fde8b7aab135c974179a2d6d90beeecc54acdffc8e7393
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp37-cp37m-manylinux2014_x86_64.whl

Download URL pysseract-1.3.1-cp37-cp37m-manylinux2014_x86_64.whl
Size 25.6 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
085cf06aecf06e03e5ecdef7fdec9b4677a29248a68aadd7e9e8054ec03c3835
BLAKE2b-256 checksum
How to use checksums
d476f3b44dfec943d8134a7c70ab273aecc855cf5284043aa58d4280007ca702
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp37-cp37m-manylinux2014_i686.whl

Download URL pysseract-1.3.1-cp37-cp37m-manylinux2014_i686.whl
Size 18.9 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
27fa256a2eb7338880722545f9a37abfaf262ee2593462a1054acaa9174b25ad
BLAKE2b-256 checksum
How to use checksums
d1c629498e76ddabd1f4fca893d2110f3ed7c5dfe0850dba31399925e1d955ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp36-cp36m-manylinux2014_x86_64.whl

Download URL pysseract-1.3.1-cp36-cp36m-manylinux2014_x86_64.whl
Size 25.6 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
afd0343cd686c861492e8f375c6020387b213fac97f845266fdace66462716c3
BLAKE2b-256 checksum
How to use checksums
faf097462e148abff0011173294221046986e729c0e4475ad2a914bcd66ee2d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp36-cp36m-manylinux2014_i686.whl

Download URL pysseract-1.3.1-cp36-cp36m-manylinux2014_i686.whl
Size 18.9 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
919f9248fa65b9eeee955a6ad1f557f6704f169eb6e71f2f5baf34ecdcad6b37
BLAKE2b-256 checksum
How to use checksums
a82a384de12812edeb62eaca08943723cda14af0f51297afc1173427e5f2a0e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp35-cp35m-manylinux2014_x86_64.whl

Download URL pysseract-1.3.1-cp35-cp35m-manylinux2014_x86_64.whl
Size 25.6 MB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e1e052e02925ee57fdec2d8d99242ac039c642e47bbc4a2e90d5aa2c8dcc790d
BLAKE2b-256 checksum
How to use checksums
8be00173942e36efb1467213f18d7bbbebdb17ad5cb39af9e65853c6cdfbaec5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release files / pysseract-1.3.1-cp35-cp35m-manylinux2014_i686.whl

Download URL pysseract-1.3.1-cp35-cp35m-manylinux2014_i686.whl
Size 18.9 MB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
f1d1aebea266baef58924a417e5cb5e8213a7e7937490ad84e256f5fdfafa000
BLAKE2b-256 checksum
How to use checksums
a28258831c00ba41319d227680160289471872b20a44064fa50bbe8645d887fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

1.3.1 This release

9 release files

1.3.0

9 release files

1.2.0

9 release files

1.1.0

9 release files

1.0.1

9 release files

1.0.0

9 release files

0.2.1

9 release files

0.2.0

9 release files

0.1.10

9 release files

0.1.9

9 release files

0.1.8

9 release files

0.1.7

9 release files

0.1.6

9 release files

0.1.5

9 release files

0.1.4

9 release files

0.1.2

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page