From 40c0acd3f25d3bbeb22bc82067e80e5b1051923b Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 28 Nov 2018 15:16:24 -0800 Subject: [PATCH] Support using --force-ocr and --threshold or --mask-barcodes together --- docs/cookbook.rst | 2 +- src/ocrmypdf/__main__.py | 5 ----- src/ocrmypdf/_pipeline.py | 41 +++++++++++++++++++-------------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/docs/cookbook.rst b/docs/cookbook.rst index 996062d9..bd3e6d39 100644 --- a/docs/cookbook.rst +++ b/docs/cookbook.rst @@ -129,7 +129,7 @@ OCRmyPDF perform some image processing on each page of a PDF, if desired. The s * ``--clean-final`` uses unpaper to clean up pages before OCR and inserts the page into the final output. You will want to review each page to ensure that unpaper did not remove something important. -* ``-mask-barcodes`` will "cover up" any barcodes detected in the image of a page. Barcodes are known to confuse Tesseract OCR and interfere with the recognition of text on the same baseline as a barcode. The output file will contain the unaltered image of the barcode. +* ``--mask-barcodes`` will "cover up" any barcodes detected in the image of a page. Barcodes are known to confuse Tesseract OCR and interfere with the recognition of text on the same baseline as a barcode. The output file will contain the unaltered image of the barcode. .. note:: diff --git a/src/ocrmypdf/__main__.py b/src/ocrmypdf/__main__.py index 84447ebe..7424cb1d 100755 --- a/src/ocrmypdf/__main__.py +++ b/src/ocrmypdf/__main__.py @@ -545,11 +545,6 @@ def check_options_ocr_behavior(options, log): raise argparse.ArgumentError( None, "Error: choose only one of --force-ocr, --skip-text, --redo-ocr.") - if options.force_ocr and any((options.mask_barcodes, options.threshold)): - raise argparse.ArgumentError( - '--force-ocr', - 'Error: --force-ocr currently may not be used with --threshold or --mask-barcodes' - ) def check_options_optimizing(options, log): diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index e5803f38..700b722e 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -598,10 +598,6 @@ def select_ocr_image( options = context.get_options() pageinfo = get_pageinfo(image, context) - if options.force_ocr: - re_symlink(image, output_file, log) - return - with Image.open(image) as im: from PIL import ImageColor from PIL import ImageDraw @@ -613,24 +609,27 @@ def select_ocr_image( xres, yres = im.info['dpi'] log.debug('resolution %r %r', xres, yres) - mask = None # Exclude both visible and invisible text from OCR - if options.redo_ocr: - mask = True # Mask visible text, but not invisible text + if not options.force_ocr: + # Do not mask text areas when forcing OCR, because we need to OCR + # all text areas + mask = None # Exclude both visible and invisible text from OCR + if options.redo_ocr: + mask = True # Mask visible text, but not invisible text - for textarea in pageinfo.get_textareas(visible=mask, corrupt=None): - # Calculate resolution based on the image size and page dimensions - # without regard whatever resolution is in pageinfo (may differ or - # be None) - bbox = [float(v) for v in textarea] - xscale, yscale = float(xres) / 72.0, float(yres) / 72.0 - pixcoords = [bbox[0] * xscale, - im.height - bbox[3] * yscale, - bbox[2] * xscale, - im.height - bbox[1] * yscale] - pixcoords = [int(round(c)) for c in pixcoords] - log.debug('blanking %r', pixcoords) - draw.rectangle(pixcoords, fill=white) - #draw.rectangle(pixcoords, outline=pink) + for textarea in pageinfo.get_textareas(visible=mask, corrupt=None): + # Calculate resolution based on the image size and page dimensions + # without regard whatever resolution is in pageinfo (may differ or + # be None) + bbox = [float(v) for v in textarea] + xscale, yscale = float(xres) / 72.0, float(yres) / 72.0 + pixcoords = [bbox[0] * xscale, + im.height - bbox[3] * yscale, + bbox[2] * xscale, + im.height - bbox[1] * yscale] + pixcoords = [int(round(c)) for c in pixcoords] + log.debug('blanking %r', pixcoords) + draw.rectangle(pixcoords, fill=white) + #draw.rectangle(pixcoords, outline=pink) if options.mask_barcodes or options.threshold: pix = leptonica.Pix.frompil(im)