diff --git a/src/ocrmypdf.py b/src/ocrmypdf.py index db730161..8f6a675c 100755 --- a/src/ocrmypdf.py +++ b/src/ocrmypdf.py @@ -28,7 +28,7 @@ import ruffus.cmdline as cmdline from .hocrtransform import HocrTransform from .pageinfo import pdf_get_all_pageinfo from .pdfa import generate_pdfa_def -from .tesseract import TESS_VERSION +from . import tesseract warnings.simplefilter('ignore', pypdf.utils.PdfReadWarning) @@ -51,11 +51,11 @@ EXIT_OTHER_ERROR=15 MINIMUM_TESS_VERSION = '3.02.02' -if TESS_VERSION < MINIMUM_TESS_VERSION: +if tesseract.VERSION < MINIMUM_TESS_VERSION: print( "Please install tesseract {0} or newer " "(currently installed version is {1})".format( - MINIMUM_TESS_VERSION, TESS_VERSION), + MINIMUM_TESS_VERSION, tesseract.VERSION), file=sys.stderr) sys.exit(EXIT_MISSING_DEPENDENCY) @@ -74,7 +74,7 @@ parser.add_argument( 'output_file', help="output searchable PDF file") parser.add_argument( - '-l', '--language', nargs='*', default=['eng'], + '-l', '--language', action='append', help="language of the file to be OCRed") preprocessing = parser.add_argument_group( @@ -135,6 +135,26 @@ debugging.add_argument( options = parser.parse_args() +# ---------- +# Languages + +if not options.language: + options.language = ['eng'] # Enforce English hegemony + +# Support v2.x "eng+deu" language syntax +if '+' in options.language[0]: + options.language = options.language[0].split('+') + +if not set(options.language).issubset(tesseract.LANGUAGES): + print( + "The installed version of tesseract does not have language " + "data for the following requested languages: ", + file=sys.stderr) + for lang in (set(options.language) - tesseract.LANGUAGES): + print(lang, file=sys.stderr) + sys.exit(EXIT_BAD_ARGS) + + # ---------- # Logging diff --git a/src/tesseract.py b/src/tesseract.py index daa5894f..9980bd3d 100644 --- a/src/tesseract.py +++ b/src/tesseract.py @@ -1,21 +1,40 @@ #!/usr/bin/env python3 -from cffi import FFI -from ctypes.util import find_library +from subprocess import Popen, PIPE, CalledProcessError import sys +import os +import re + + +def _version(): + args_tess = [ + 'tesseract', + '--version' + ] + p_tess = Popen(args_tess, close_fds=True, universal_newlines=True, + stdout=PIPE, stderr=PIPE) + _, versions = p_tess.communicate(timeout=5) + + tesseract_version = re.match(r'tesseract\s(.+)', versions).group(1) + return tesseract_version + + +def _languages(): + args_tess = [ + 'tesseract', + '--list-langs' + ] + p_tess = Popen(args_tess, close_fds=True, universal_newlines=True, + stdout=PIPE, stderr=PIPE) + _, langs = p_tess.communicate(timeout=5) + + return set(lang.strip() for lang in langs.splitlines()[1:]) -ffi = FFI() try: - libtess = ffi.dlopen(find_library('libtesseract')) -except Exception: - print("Could not find Tesseract 3.02.02", file=sys.stderr) + VERSION = _version() + LANGUAGES = _languages() +except Exception as e: + print(e) + print("Could not find tesseract executable", file=sys.stderr) + sys.exit(1) - -ffi.cdef(''' - const char* TessVersion(); -''') - -cstr_version = libtess.TessVersion() -TESS_VERSION = ffi.string(cstr_version).decode('ascii') - -__all__ = ['TESS_VERSION']