pysseract.Pysseract

class pysseract.Pysseract

This is the main class for interacting with the Tesseract API. There are several ways to initialise this class. The simplest way with all defaults and assuming that the English model is needed is as follows:

import pysseract
with pysseract.Pysseract() as t:
    t.SetImageFromPath('/Users/hxia/Downloads/001-helloworld.png')
    print(t.utf8Text)

In order to set a language (such as French in this example), and model location (‘/Users/shogg/models’), you might choose to initialise in the below manner instead:

t = pysseract.Pysseract('/Users/shogg/models', 'fra')

To initialise with French and Arabic you would run the following command:

t = pysseract.Pysseract('/Users/shogg/models', 'fra+ara')

It’s also possible to set an OCR mode as well on initialisation, though there are ways to do that afterwards. If for any reason you need to perform detailed initialization for the base API, you can use the third signature listed below. two conversions have been done as follows. As part of that initialisation signature, two type conversions will occur, as follows:

  1. char **configs, int configs_size <==> configsList

  2. const GenericVector<STRING> *vars_vec, const GenericVector<STRING> *vars_values <==> settingDict

configsList is a list of files from which configuration variables can be read settingDict is a dict object containing parameters and their values to be fed to Tesseract. You will typically not need both of these things, in which case one can be set as an empty List (or Dict) as the case may be.

For information about working with the results of analysis, please see the documentation for ResultIterator or Pysseract.IterAt

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: _pysseract.Pysseract) -> None

  2. __init__(self: _pysseract.Pysseract, datapath: str, language: str) -> None

  3. __init__(self: _pysseract.Pysseract, datapath: str, language: str, engineMode: tesseract::OcrEngineMode) -> None

  4. __init__(self: _pysseract.Pysseract, datapath: str, language: str, engineMode: tesseract::OcrEngineMode, configsList: List[str], settingDict: Dict[str, str], setOnlyNonDebugParams: bool) -> None

Attributes

Pysseract.dataPath

Read-only: Returns the path where Tesseract model objects are stored

Pysseract.pageSegMode

This attribute can be used to get or set the page segmentation mode used by the tesseract model

Pysseract.unlvText

Read-only: Return all identified text according to UNLV format Latin-1 with specific reject and suspect codes

Pysseract.utf8Text

Read-only: Return all identified text concatenated into a UTF-8 string

Methods

Pysseract.Clear(self)

Free up recognition results and any stored image data, without actually freeing any recognition data that would be time-consuming to reload.

Pysseract.End(self)

Close down tesseract and free up all memory, after which the instance should not be reused.

Pysseract.GetAltoText(self, pagenum)

Make an ALTO XML string from internal data.

Pysseract.GetHOCRText(self, pagenum)

Make an HTML-formatted string with hOCR.

Pysseract.GetInputImage(self)

Return the source image being considered by Tesseract

Pysseract.GetIterator(self)

Returns the iterator over boxes found in a given source image

Pysseract.GetLSTMBoxText(self, pagenum)

Make a box file for LSTM training from the internal data structures.

Pysseract.GetOsdText(self, pagenum)

Recognised text is returned as UTF-8.

Pysseract.GetTSVText(self, pagenum)

Make a TSV-formatted string from the internal data structures.

Pysseract.GetThresholdedImage(self)

Get a copy of the image Tesseract has after pre-processing is complete.

Pysseract.GetVariableAsString(self, arg0)

Get value of named variable as a string, if it exists.

Pysseract.GetWordStrBoxText(self, pagenum)

Make a string formatted in the same style as Tesseract training data.

Pysseract.IterAt(level)

Returns a generator that allows you to iterate through all the results at a specified PageIteratorLevel

Pysseract.SetImageFromBytes(self, bytes)

Read an image from a string of bytes

Pysseract.SetImageFromPath(self, imgpath)

Read an image from a given fully-qualified file path

Pysseract.SetRectangle(self, left, top, …)

Restrict recognition to a sub-rectangle of the image.

Pysseract.SetSourceResolution(self, ppi)

Set the pixel-per-inch value for the source image

Pysseract.SetVariable(self, name, value)

Note: Must be called after Init().