From 91b42cbfa855972112492699297a44c6806c9804 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 9 Jan 2018 00:17:53 -0800 Subject: [PATCH] Fix issue in sandwich renderer when skipping OCR on a rotated and deskewed page If OCR is skipped due to --tesseract-timeout or similar, and the skip page is rotated with /Rotate, and the skip page was deskewed or had other image processing, then the skip page was created with the wrong dimensions causing the output page to be cropped. --- ocrmypdf/exec/ghostscript.py | 2 +- ocrmypdf/exec/tesseract.py | 4 ++++ tests/test_main.py | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ocrmypdf/exec/ghostscript.py b/ocrmypdf/exec/ghostscript.py index b94c56fb..d77efecc 100644 --- a/ocrmypdf/exec/ghostscript.py +++ b/ocrmypdf/exec/ghostscript.py @@ -37,7 +37,7 @@ def rasterize_pdf(input_file, output_file, xres, yres, raster_device, log, :param yres: :param raster_device: :param log: - :param pageno: page number to rasterize + :param pageno: page number to rasterize (beginning at page 1) :param page_dpi: resolution tuple (x, y) overriding output image DPI :return: """ diff --git a/ocrmypdf/exec/tesseract.py b/ocrmypdf/exec/tesseract.py index e4cf9600..1ead0c6b 100644 --- a/ocrmypdf/exec/tesseract.py +++ b/ocrmypdf/exec/tesseract.py @@ -279,6 +279,10 @@ def use_skip_page(text_only, skip_pdf, output_pdf, output_text): with open(output_pdf, 'wb') as out: pdf_out = pypdf.PdfFileWriter() w, h = page0.mediaBox.getWidth(), page0.mediaBox.getHeight() + # If skip page has a /Rotate key, replicate the rotation + rotation = int(page0.get('/Rotate', 0)) + if rotation % 180 == 90: + w, h = h, w pdf_out.addBlankPage(w, h) pdf_out.write(out) diff --git a/tests/test_main.py b/tests/test_main.py index c21f2253..b6c652b0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1037,3 +1037,26 @@ def test_bad_utf8(spoof_tess_bad_utf8, renderer, resources, no_outpdf): assert p.returncode != 0 assert 'not utf-8' in err, "should whine about utf-8" assert '\\x96' in err, 'should repeat backslash encoded output' + + +@pytest.mark.skipif( + not tesseract.has_textonly_pdf(), + reason="issue only affects sandwich") +def test_rotate_deskew_timeout(resources, outdir): + check_ocrmypdf( + resources / 'rotated_skew.pdf', + outdir / 'deskewed.pdf', + '--deskew', + '--tesseract-timeout', '0', + '--pdf-renderer', 'sandwich' + ) + + correlation = check_monochrome_correlation( + outdir, + reference_pdf=resources / 'ccitt.pdf', + reference_pageno=1, + test_pdf=outdir / 'deskewed.pdf', + test_pageno=1) + + # Confirm that the page still got deskewed + assert correlation > 0.50 \ No newline at end of file