From bb91393b8518c5b7154b2634bb9eb2934ea54584 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 7 Nov 2016 14:17:31 -0800 Subject: [PATCH] =?UTF-8?q?Fix=20=E2=80=9Cdeskew-rotate=E2=80=9D=20bug.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Turns out this occurred in any case where pdf-renderer hocr was used and a tesseract timeout or error occurred. We created a replacement page based on the unrotated page dimensions instead of the input image’s dimensions. --- ocrmypdf/tesseract.py | 17 +++++++++++------ tests/test_main.py | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ocrmypdf/tesseract.py b/ocrmypdf/tesseract.py index abc62a89..ec315b69 100644 --- a/ocrmypdf/tesseract.py +++ b/ocrmypdf/tesseract.py @@ -147,11 +147,16 @@ def page_timedout(log, input_file): log.warning(prefix + " took too long to OCR - skipping") -def _generate_null_hocr(output_hocr, pageinfo): +def _generate_null_hocr(output_hocr, image): + """Produce a .hocr file that reports no text detected on a page that is + the same size as the input image.""" + from PIL import Image + + im = Image.open(image) + w, h = im.size + with open(output_hocr, 'w', encoding="utf-8") as f: - f.write(HOCR_TEMPLATE.format( - pageinfo['width_pixels'], - pageinfo['height_pixels'])) + f.write(HOCR_TEMPLATE.format(w, h)) def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, @@ -181,11 +186,11 @@ def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, # Temporary workaround to hocrTransform not being able to function if # it does not have a valid hOCR file. page_timedout(log, input_file) - _generate_null_hocr(output_hocr, pageinfo_getter()) + _generate_null_hocr(output_hocr, input_file) except CalledProcessError as e: tesseract_log_output(log, e.output, input_file) if 'Image too large' in e.output: - _generate_null_hocr(output_hocr, pageinfo_getter()) + _generate_null_hocr(output_hocr, input_file) return raise e from e diff --git a/tests/test_main.py b/tests/test_main.py index e528a74a..cdd67638 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -707,6 +707,7 @@ def test_rotated_skew_timeout(): out = check_ocrmypdf( 'rotated_skew.pdf', 'test_rotated_skew.pdf', + '--pdf-renderer', 'hocr', '--deskew', '--tesseract-timeout', '0') out_pageinfo = pdf_get_all_pageinfo(out)[0]