Hello community, here is the log from the commit of package python-pytesseract for openSUSE:Factory checked in at 2019-09-11 10:36:29 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/python-pytesseract (Old) and /work/SRC/openSUSE:Factory/.python-pytesseract.new.7948 (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "python-pytesseract" Wed Sep 11 10:36:29 2019 rev:6 rq:729832 version:0.3.0 Changes: -------- --- /work/SRC/openSUSE:Factory/python-pytesseract/python-pytesseract.changes 2019-07-22 17:20:25.721893847 +0200 +++ /work/SRC/openSUSE:Factory/.python-pytesseract.new.7948/python-pytesseract.changes 2019-09-11 10:36:33.519275925 +0200 @@ -1,0 +2,6 @@ +Tue Sep 10 11:35:38 UTC 2019 - Tomáš Chvátal <[email protected]> + +- Update to 0.3.0: + * no upstream changelog + +------------------------------------------------------------------- Old: ---- pytesseract-0.2.7.tar.gz New: ---- pytesseract-0.3.0.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ python-pytesseract.spec ++++++ --- /var/tmp/diff_new_pack.m619yR/_old 2019-09-11 10:36:34.195275728 +0200 +++ /var/tmp/diff_new_pack.m619yR/_new 2019-09-11 10:36:34.195275728 +0200 @@ -18,7 +18,7 @@ %{?!python_module:%define python_module() python-%{**} python3-%{**}} Name: python-pytesseract -Version: 0.2.7 +Version: 0.3.0 Release: 0 Summary: Python wrapper for Google's Tesseract-OCR License: GPL-3.0-only ++++++ pytesseract-0.2.7.tar.gz -> pytesseract-0.3.0.tar.gz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/MANIFEST.in new/pytesseract-0.3.0/MANIFEST.in --- old/pytesseract-0.2.7/MANIFEST.in 2018-05-31 03:28:27.000000000 +0200 +++ new/pytesseract-0.3.0/MANIFEST.in 2019-08-16 02:44:25.000000000 +0200 @@ -1,4 +1,2 @@ -recursive-include src *.jpg -recursive-include src *.png include LICENSE include README.rst diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/PKG-INFO new/pytesseract-0.3.0/PKG-INFO --- old/pytesseract-0.2.7/PKG-INFO 2019-06-19 04:40:12.000000000 +0200 +++ new/pytesseract-0.3.0/PKG-INFO 2019-08-23 03:04:00.000000000 +0200 @@ -1,15 +1,24 @@ -Metadata-Version: 1.1 +Metadata-Version: 1.2 Name: pytesseract -Version: 0.2.7 +Version: 0.3.0 Summary: Python-tesseract is a python wrapper for Google's Tesseract-OCR Home-page: https://github.com/madmaze/python-tesseract -Author: Matthias Lee -Author-email: [email protected] +Author: Samuel Hoffstaetter +Author-email: [email protected] +Maintainer: Matthias Lee +Maintainer-email: [email protected] License: GPLv3 -Description-Content-Type: UNKNOWN Description: Python Tesseract ================ + .. image:: https://travis-ci.org/madmaze/pytesseract.svg + :target: https://travis-ci.org/madmaze/pytesseract + :alt: Travis build status + + .. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg + :target: https://pypi.python.org/pypi/pytesseract + :alt: Python versions + .. image:: https://img.shields.io/pypi/v/pytesseract.svg :target: https://pypi.python.org/pypi/pytesseract :alt: PyPI release @@ -18,18 +27,17 @@ :target: https://github.com/madmaze/pytesseract/releases :alt: Github release - .. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg - :target: https://pypi.python.org/pypi/pytesseract - :alt: PyPI version + .. image:: https://anaconda.org/conda-forge/pytesseract/badges/version.svg + :target: https://anaconda.org/conda-forge/pytesseract + :alt: Conda release 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 <https://github.com/tesseract-ocr/tesseract>`_. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types - supported by the Python Imaging Library, including jpeg, png, gif, bmp, tiff, - and others, whereas tesseract-ocr by default only supports tiff and bmp. - Additionally, if used as a script, Python-tesseract will print the recognized + 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 @@ -37,6 +45,8 @@ **Quickstart** + *Note*: Test images are located in the ``tests/data`` folder of the Git repo. + .. code-block:: python try: @@ -88,6 +98,14 @@ # OR explicit beforehand converting print(pytesseract.image_to_string(Image.fromarray(img)) + 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) + Add the following config, if you have tessdata error like: "Error opening data file..." .. code-block:: python @@ -109,9 +127,11 @@ * **image_to_osd** Returns result containing information about orientation and script detection. + * **run_and_get_output** Returns the raw output from Tesseract OCR. Gives a bit more control over the parameters that are sent to tesseract. + **Parameters** - ``image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)`` + ``image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0)`` * **image** Object, PIL Image/NumPy array of the image to be processed by Tesseract @@ -123,13 +143,15 @@ * **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 <https://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py>`_ class. + * **timeout** Integer or Float, duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError. + INSTALLATION ------------ Prerequisites: - - Python-tesseract requires python 2.7+ or python 3.x + - Python-tesseract requires Python 2.7 or Python 3.5+ - You will need the Python Imaging Library (PIL) (or the `Pillow <https://pypi.org/project/Pillow/>`_ fork). Under Debian/Ubuntu, this is the package **python-imaging** or **python3-imaging**. - Install `Google Tesseract OCR <https://github.com/tesseract-ocr/tesseract>`_ @@ -161,6 +183,23 @@ $> git clone https://github.com/madmaze/pytesseract.git $ (env)> cd pytesseract && pip install -U . + | Install with conda (via `conda-forge <https://anaconda.org/conda-forge/pytesseract>`_): + + .. 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 + + $ (env)> pip install tox + $ (env)> tox + LICENSE ------- Python-tesseract is released under the GPL v3. @@ -172,10 +211,17 @@ - `Matthias Lee <https://github.com/madmaze>`_ - `Lars Kistner <https://github.com/Sr4l>`_ - `Ryan Mitchell <https://github.com/REMitchell>`_ - - `Emilio Cecchini <https://github.com/check-emee>`_ + - `Emilio Cecchini <https://github.com/ceccoemi>`_ + - `John Hagen <https://github.com/johnthagen>`_ + - `Darius Morawiec <https://github.com/nok>`_ + - `Eddie Bedada <https://github.com/adbeda>`_ Keywords: python-tesseract OCR Python Platform: UNKNOWN Classifier: Programming Language :: Python +Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.5 +Classifier: Programming Language :: Python :: 3.6 +Classifier: Programming Language :: Python :: 3.7 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/README.rst new/pytesseract-0.3.0/README.rst --- old/pytesseract-0.2.7/README.rst 2019-06-19 04:40:00.000000000 +0200 +++ new/pytesseract-0.3.0/README.rst 2019-08-23 03:03:47.000000000 +0200 @@ -1,6 +1,14 @@ Python Tesseract ================ +.. image:: https://travis-ci.org/madmaze/pytesseract.svg + :target: https://travis-ci.org/madmaze/pytesseract + :alt: Travis build status + +.. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg + :target: https://pypi.python.org/pypi/pytesseract + :alt: Python versions + .. image:: https://img.shields.io/pypi/v/pytesseract.svg :target: https://pypi.python.org/pypi/pytesseract :alt: PyPI release @@ -9,18 +17,17 @@ :target: https://github.com/madmaze/pytesseract/releases :alt: Github release -.. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg - :target: https://pypi.python.org/pypi/pytesseract - :alt: PyPI version +.. image:: https://anaconda.org/conda-forge/pytesseract/badges/version.svg + :target: https://anaconda.org/conda-forge/pytesseract + :alt: Conda release 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 <https://github.com/tesseract-ocr/tesseract>`_. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types -supported by the Python Imaging Library, including jpeg, png, gif, bmp, tiff, -and others, whereas tesseract-ocr by default only supports tiff and bmp. -Additionally, if used as a script, Python-tesseract will print the recognized +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 @@ -28,6 +35,8 @@ **Quickstart** +*Note*: Test images are located in the ``tests/data`` folder of the Git repo. + .. code-block:: python try: @@ -79,6 +88,14 @@ # OR explicit beforehand converting print(pytesseract.image_to_string(Image.fromarray(img)) +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) + Add the following config, if you have tessdata error like: "Error opening data file..." .. code-block:: python @@ -100,9 +117,11 @@ * **image_to_osd** Returns result containing information about orientation and script detection. +* **run_and_get_output** Returns the raw output from Tesseract OCR. Gives a bit more control over the parameters that are sent to tesseract. + **Parameters** -``image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)`` +``image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0)`` * **image** Object, PIL Image/NumPy array of the image to be processed by Tesseract @@ -114,13 +133,15 @@ * **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 <https://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py>`_ class. +* **timeout** Integer or Float, duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError. + INSTALLATION ------------ Prerequisites: -- Python-tesseract requires python 2.7+ or python 3.x +- Python-tesseract requires Python 2.7 or Python 3.5+ - You will need the Python Imaging Library (PIL) (or the `Pillow <https://pypi.org/project/Pillow/>`_ fork). Under Debian/Ubuntu, this is the package **python-imaging** or **python3-imaging**. - Install `Google Tesseract OCR <https://github.com/tesseract-ocr/tesseract>`_ @@ -152,6 +173,23 @@ $> git clone https://github.com/madmaze/pytesseract.git $ (env)> cd pytesseract && pip install -U . +| Install with conda (via `conda-forge <https://anaconda.org/conda-forge/pytesseract>`_): + +.. 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 + + $ (env)> pip install tox + $ (env)> tox + LICENSE ------- Python-tesseract is released under the GPL v3. @@ -163,4 +201,7 @@ - `Matthias Lee <https://github.com/madmaze>`_ - `Lars Kistner <https://github.com/Sr4l>`_ - `Ryan Mitchell <https://github.com/REMitchell>`_ -- `Emilio Cecchini <https://github.com/check-emee>`_ +- `Emilio Cecchini <https://github.com/ceccoemi>`_ +- `John Hagen <https://github.com/johnthagen>`_ +- `Darius Morawiec <https://github.com/nok>`_ +- `Eddie Bedada <https://github.com/adbeda>`_ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/pytesseract.egg-info/PKG-INFO new/pytesseract-0.3.0/pytesseract.egg-info/PKG-INFO --- old/pytesseract-0.2.7/pytesseract.egg-info/PKG-INFO 2019-06-19 04:40:12.000000000 +0200 +++ new/pytesseract-0.3.0/pytesseract.egg-info/PKG-INFO 2019-08-23 03:04:00.000000000 +0200 @@ -1,15 +1,24 @@ -Metadata-Version: 1.1 +Metadata-Version: 1.2 Name: pytesseract -Version: 0.2.7 +Version: 0.3.0 Summary: Python-tesseract is a python wrapper for Google's Tesseract-OCR Home-page: https://github.com/madmaze/python-tesseract -Author: Matthias Lee -Author-email: [email protected] +Author: Samuel Hoffstaetter +Author-email: [email protected] +Maintainer: Matthias Lee +Maintainer-email: [email protected] License: GPLv3 -Description-Content-Type: UNKNOWN Description: Python Tesseract ================ + .. image:: https://travis-ci.org/madmaze/pytesseract.svg + :target: https://travis-ci.org/madmaze/pytesseract + :alt: Travis build status + + .. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg + :target: https://pypi.python.org/pypi/pytesseract + :alt: Python versions + .. image:: https://img.shields.io/pypi/v/pytesseract.svg :target: https://pypi.python.org/pypi/pytesseract :alt: PyPI release @@ -18,18 +27,17 @@ :target: https://github.com/madmaze/pytesseract/releases :alt: Github release - .. image:: https://img.shields.io/pypi/pyversions/pytesseract.svg - :target: https://pypi.python.org/pypi/pytesseract - :alt: PyPI version + .. image:: https://anaconda.org/conda-forge/pytesseract/badges/version.svg + :target: https://anaconda.org/conda-forge/pytesseract + :alt: Conda release 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 <https://github.com/tesseract-ocr/tesseract>`_. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types - supported by the Python Imaging Library, including jpeg, png, gif, bmp, tiff, - and others, whereas tesseract-ocr by default only supports tiff and bmp. - Additionally, if used as a script, Python-tesseract will print the recognized + 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 @@ -37,6 +45,8 @@ **Quickstart** + *Note*: Test images are located in the ``tests/data`` folder of the Git repo. + .. code-block:: python try: @@ -88,6 +98,14 @@ # OR explicit beforehand converting print(pytesseract.image_to_string(Image.fromarray(img)) + 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) + Add the following config, if you have tessdata error like: "Error opening data file..." .. code-block:: python @@ -109,9 +127,11 @@ * **image_to_osd** Returns result containing information about orientation and script detection. + * **run_and_get_output** Returns the raw output from Tesseract OCR. Gives a bit more control over the parameters that are sent to tesseract. + **Parameters** - ``image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)`` + ``image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0)`` * **image** Object, PIL Image/NumPy array of the image to be processed by Tesseract @@ -123,13 +143,15 @@ * **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 <https://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py>`_ class. + * **timeout** Integer or Float, duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError. + INSTALLATION ------------ Prerequisites: - - Python-tesseract requires python 2.7+ or python 3.x + - Python-tesseract requires Python 2.7 or Python 3.5+ - You will need the Python Imaging Library (PIL) (or the `Pillow <https://pypi.org/project/Pillow/>`_ fork). Under Debian/Ubuntu, this is the package **python-imaging** or **python3-imaging**. - Install `Google Tesseract OCR <https://github.com/tesseract-ocr/tesseract>`_ @@ -161,6 +183,23 @@ $> git clone https://github.com/madmaze/pytesseract.git $ (env)> cd pytesseract && pip install -U . + | Install with conda (via `conda-forge <https://anaconda.org/conda-forge/pytesseract>`_): + + .. 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 + + $ (env)> pip install tox + $ (env)> tox + LICENSE ------- Python-tesseract is released under the GPL v3. @@ -172,10 +211,17 @@ - `Matthias Lee <https://github.com/madmaze>`_ - `Lars Kistner <https://github.com/Sr4l>`_ - `Ryan Mitchell <https://github.com/REMitchell>`_ - - `Emilio Cecchini <https://github.com/check-emee>`_ + - `Emilio Cecchini <https://github.com/ceccoemi>`_ + - `John Hagen <https://github.com/johnthagen>`_ + - `Darius Morawiec <https://github.com/nok>`_ + - `Eddie Bedada <https://github.com/adbeda>`_ Keywords: python-tesseract OCR Python Platform: UNKNOWN Classifier: Programming Language :: Python +Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.5 +Classifier: Programming Language :: Python :: 3.6 +Classifier: Programming Language :: Python :: 3.7 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/pytesseract.egg-info/SOURCES.txt new/pytesseract-0.3.0/pytesseract.egg-info/SOURCES.txt --- old/pytesseract-0.2.7/pytesseract.egg-info/SOURCES.txt 2019-06-19 04:40:12.000000000 +0200 +++ new/pytesseract-0.3.0/pytesseract.egg-info/SOURCES.txt 2019-08-23 03:04:00.000000000 +0200 @@ -10,6 +10,4 @@ pytesseract.egg-info/requires.txt pytesseract.egg-info/top_level.txt src/__init__.py -src/pytesseract.py -src/test-european.jpg -src/test.png \ No newline at end of file +src/pytesseract.py \ No newline at end of file diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/setup.py new/pytesseract-0.3.0/setup.py --- old/pytesseract-0.2.7/setup.py 2019-06-19 04:40:00.000000000 +0200 +++ new/pytesseract-0.3.0/setup.py 2019-08-23 03:03:47.000000000 +0200 @@ -14,7 +14,7 @@ setup( name=PACKAGE_NAME, - version='0.2.7', + version='0.3.0', author='Samuel Hoffstaetter', author_email='[email protected]', maintainer='Matthias Lee', @@ -35,7 +35,11 @@ }, classifiers=[ 'Programming Language :: Python', + 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', ] ) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/src/__init__.py new/pytesseract-0.3.0/src/__init__.py --- old/pytesseract-0.2.7/src/__init__.py 2018-10-05 03:46:37.000000000 +0200 +++ new/pytesseract-0.3.0/src/__init__.py 2019-08-16 02:46:52.000000000 +0200 @@ -1,10 +1,13 @@ -from .pytesseract import ( +from .pytesseract import ( # noqa: F401 + Output, + TesseractError, + TesseractNotFoundError, + TSVNotSupported, get_tesseract_version, - image_to_string, - image_to_data, image_to_boxes, + image_to_data, image_to_osd, image_to_pdf_or_hocr, - TesseractError, - Output + image_to_string, + run_and_get_output ) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pytesseract-0.2.7/src/pytesseract.py new/pytesseract-0.3.0/src/pytesseract.py --- old/pytesseract-0.2.7/src/pytesseract.py 2019-06-19 04:40:00.000000000 +0200 +++ new/pytesseract-0.3.0/src/pytesseract.py 2019-08-23 03:03:47.000000000 +0200 @@ -1,32 +1,34 @@ #!/usr/bin/env python -''' +""" Python-tesseract. For more information: https://github.com/madmaze/pytesseract -''' - -try: - from PIL import Image -except ImportError: - import Image +""" import os -import sys -import subprocess -import tempfile import shlex import string -from glob import iglob +import subprocess +import sys +import tempfile +from contextlib import contextmanager from csv import QUOTE_NONE +from distutils.version import LooseVersion from functools import wraps +from glob import iglob +from io import BytesIO +from os.path import normcase, normpath, realpath from pkgutil import find_loader -from distutils.version import LooseVersion -from os.path import realpath, normpath, normcase +from threading import Timer + +try: + from PIL import Image +except ImportError: + import Image numpy_installed = find_loader('numpy') is not None if numpy_installed: from numpy import ndarray -from io import BytesIO pandas_installed = find_loader('pandas') is not None if pandas_installed: import pandas as pd @@ -34,6 +36,10 @@ # CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY tesseract_cmd = 'tesseract' RGB_MODE = 'RGB' +SUPPORTED_FORMATS = { + 'JPEG', 'PNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'BMP', 'GIF' +} + OSD_KEYS = { 'Page number': ('page_num', int), 'Orientation in degrees': ('orientation', int), @@ -77,6 +83,34 @@ ) +def kill(process, code): + process.kill() + process.returncode = code + + +@contextmanager +def timeout_manager(proc, seconds=0): + try: + if not seconds: + yield proc.communicate()[1] + return + + timeout_code = -1 + timer = Timer(seconds, kill, [proc, timeout_code]) + timer.start() + try: + _, error_string = proc.communicate() + yield error_string + finally: + timer.cancel() + if proc.returncode is timeout_code and not error_string: + raise RuntimeError('Tesseract process timeout') + finally: + proc.stdin.close() + proc.stdout.close() + proc.stderr.close() + + def run_once(func): @wraps(func) def wrapper(*args, **kwargs): @@ -95,7 +129,7 @@ def cleanup(temp_name): - ''' Tries to remove temp files by filename wildcard path. ''' + """ Tries to remove temp files by filename wildcard path. """ for filename in iglob(temp_name + '*' if temp_name else temp_name): try: os.remove(filename) @@ -104,24 +138,15 @@ def prepare(image): - if isinstance(image, Image.Image): - return image - if numpy_installed and isinstance(image, ndarray): - return Image.fromarray(image) - - raise TypeError('Unsupported image object') + image = Image.fromarray(image) + if not isinstance(image, Image.Image): + raise TypeError('Unsupported image object') -def save_image(image): - temp_name = tempfile.mktemp(prefix='tess_') - if isinstance(image, str): - return temp_name, realpath(normpath(normcase(image))) - - image = prepare(image) - img_extension = image.format - if image.format not in {'JPEG', 'PNG', 'TIFF', 'BMP', 'GIF'}: - img_extension = 'PNG' + extension = 'PNG' if not image.format else image.format + if extension not in SUPPORTED_FORMATS: + raise TypeError('Unsupported image format/type') if not image.mode.startswith(RGB_MODE): image = image.convert(RGB_MODE) @@ -132,8 +157,20 @@ background.paste(image, (0, 0), image) image = background - input_file_name = temp_name + os.extsep + img_extension - image.save(input_file_name, format=img_extension, **image.info) + image.format = extension + return image, extension + + +def save_image(image): + with tempfile.NamedTemporaryFile(prefix='tess_', delete=False) as f: + temp_name = f.name + + if isinstance(image, str): + return temp_name, realpath(normpath(normcase(image))) + + image, extension = prepare(image) + input_file_name = temp_name + os.extsep + extension + image.save(input_file_name, format=extension, **image.info) return temp_name, input_file_name @@ -164,7 +201,8 @@ extension, lang, config='', - nice=0): + nice=0, + timeout=0): cmd_args = [] if not sys.platform.startswith('win32') and nice != 0: @@ -175,9 +213,10 @@ if lang is not None: cmd_args += ('-l', lang) - cmd_args += shlex.split(config) + if config: + cmd_args += shlex.split(config) - if extension not in {'box', 'osd', 'tsv'}: + if extension and extension not in {'box', 'osd', 'tsv'}: cmd_args.append(extension) try: @@ -185,22 +224,17 @@ except OSError: raise TesseractNotFoundError() - try: - _, error_string = proc.communicate() - finally: - proc.stdin.close() - proc.stdout.close() - proc.stderr.close() - - if proc.returncode: - raise TesseractError(proc.returncode, get_errors(error_string)) + with timeout_manager(proc, timeout) as error_string: + if proc.returncode: + raise TesseractError(proc.returncode, get_errors(error_string)) def run_and_get_output(image, - extension, + extension='', lang=None, config='', nice=0, + timeout=0, return_bytes=False): temp_name, input_filename = '', '' @@ -212,7 +246,8 @@ 'extension': extension, 'lang': lang, 'config': config, - 'nice': nice + 'nice': nice, + 'timeout': timeout } run_tesseract(**kwargs) @@ -279,9 +314,9 @@ @run_once def get_tesseract_version(): - ''' + """ Returns LooseVersion object of the Tesseract version - ''' + """ try: return LooseVersion( subprocess.check_output( @@ -296,11 +331,12 @@ lang=None, config='', nice=0, - output_type=Output.STRING): - ''' + output_type=Output.STRING, + timeout=0): + """ Returns the result of a Tesseract OCR run on the provided image to string - ''' - args = [image, 'txt', lang, config, nice] + """ + args = [image, 'txt', lang, config, nice, timeout] return { Output.BYTES: lambda: run_and_get_output(*(args + [True])), @@ -310,17 +346,18 @@ def image_to_pdf_or_hocr(image, - lang=None, - config='', - nice=0, - extension='pdf'): - ''' + lang=None, + config='', + nice=0, + extension='pdf', + timeout=0): + """ Returns the result of a Tesseract OCR run on the provided image to pdf/hocr - ''' + """ if extension not in {'pdf', 'hocr'}: raise ValueError('Unsupported extension: {}'.format(extension)) - args = [image, extension, lang, config, nice, True] + args = [image, extension, lang, config, nice, timeout, True] return run_and_get_output(*args) @@ -329,12 +366,13 @@ lang=None, config='', nice=0, - output_type=Output.STRING): - ''' + output_type=Output.STRING, + timeout=0): + """ Returns string containing recognized characters and their box boundaries - ''' + """ config += ' batch.nochop makebox' - args = [image, 'box', lang, config, nice] + args = [image, 'box', lang, config, nice, timeout] return { Output.BYTES: lambda: run_and_get_output(*(args + [True])), @@ -361,17 +399,18 @@ lang=None, config='', nice=0, - output_type=Output.STRING): - ''' + output_type=Output.STRING, + timeout=0): + """ Returns string containing box boundaries, confidences, and other information. Requires Tesseract 3.05+ - ''' + """ if get_tesseract_version() < '3.05': raise TSVNotSupported() config = '{} {}'.format('-c tessedit_create_tsv=1', config.strip()).strip() - args = [image, 'tsv', lang, config, nice] + args = [image, 'tsv', lang, config, nice, timeout] return { Output.BYTES: lambda: run_and_get_output(*(args + [True])), @@ -385,15 +424,16 @@ lang='osd', config='', nice=0, - output_type=Output.STRING): - ''' + output_type=Output.STRING, + timeout=0): + """ Returns string containing the orientation and script detection (OSD) - ''' + """ config = '{}-psm 0 {}'.format( '' if get_tesseract_version() < '3.05' else '-', config.strip() ).strip() - args = [image, 'osd', lang, config, nice] + args = [image, 'osd', lang, config, nice, timeout] return { Output.BYTES: lambda: run_and_get_output(*(args + [True])), Binary files old/pytesseract-0.2.7/src/test-european.jpg and new/pytesseract-0.3.0/src/test-european.jpg differ Binary files old/pytesseract-0.2.7/src/test.png and new/pytesseract-0.3.0/src/test.png differ
