From 3731fdfd72cd0f165d952d3fcba01946e29001d4 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sun, 16 Apr 2023 01:41:19 -0700 Subject: [PATCH] Add --tesseract-downsample-above X --- src/ocrmypdf/builtin_plugins/tesseract_ocr.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ocrmypdf/builtin_plugins/tesseract_ocr.py b/src/ocrmypdf/builtin_plugins/tesseract_ocr.py index 7de1bfca..6c530eca 100644 --- a/src/ocrmypdf/builtin_plugins/tesseract_ocr.py +++ b/src/ocrmypdf/builtin_plugins/tesseract_ocr.py @@ -102,6 +102,20 @@ def add_options(parser): "of --tesseract-timeout to ensure Tesseract has enough to time." ), ) + tess.add_argument( + '--tesseract-downsample-above', + action='store', + type=numeric(int, 100, 32767), + default=32767, + help=( + "Downsample images larger than this size pixel size in either dimension " + "before OCR. --tesseract-downsample-large-images downsamples only when " + "an image exceeds Tesseract's internal limits. This argument causes " + "downsampling to occur when an image exceeds the given size. This may " + "reduce OCR quality, but on large images the most desirable text is " + "usually larger." + ), + ) tess.add_argument( '--user-words', metavar='FILE', @@ -170,10 +184,12 @@ def filter_ocr_image(page: PageContext, image: Image.Image) -> Image.Image: or more than 2**31 bytes. This function resizes the image to fit within those limits. """ + threshold = min(page.options.tesseract_downsample_above, 32767) + options = page.options if options.tesseract_downsample_large_images: size = calculate_downsample( - image, max_size=(32767, 32767), max_bytes=(2**31) - 1 + image, max_size=(threshold, threshold), max_bytes=(2**31) - 1 ) image = downsample_image(image, size) return image