diff --git a/ocrmypdf/pageinfo.py b/ocrmypdf/pageinfo.py index 9470f37c..418ca991 100644 --- a/ocrmypdf/pageinfo.py +++ b/ocrmypdf/pageinfo.py @@ -376,6 +376,11 @@ def _pdf_get_pageinfo(infile, pageno: int): pageinfo['width_inches'] = width_pt / Decimal(72.0) pageinfo['height_inches'] = height_pt / Decimal(72.0) + try: + pageinfo['rotate'] = int(page['/Rotate']) + except KeyError: + pageinfo['rotate'] = 0 + try: contentstream = pypdf.pdf.ContentStream(page.getContents(), pdf) except AttributeError as e: diff --git a/tests/resources/README.rst b/tests/resources/README.rst index 5f227c90..fdb0af74 100644 --- a/tests/resources/README.rst +++ b/tests/resources/README.rst @@ -82,14 +82,14 @@ Assemblies These test resources are assemblies from other previously mentioned files, released under the same license terms as their input files. -- cardinal.pdf (four cardinal directions, rotated copies of LinnSequencer.jpg) +- cardinal.pdf (four cardinal directions, baked-in rotated copies of LinnSequencer.jpg) - ccitt.pdf (LinnSequencer.jpg, converted to CCITT encoding) - encrypted_algo4.pdf (congress.jpg, encrypted with algorithm 4 - not supported by PyPDF2) - graph_ocred.pdf (from graph.pdf) - jbig2.pdf (congress.jpg, converted to JBIG2 encoding) - multipage.pdf (from several other files) - palette.pdf (congress.jpg, converted to a 256-color palette) -- skew.pdf (from c02-22.pdf) +- skew.pdf (from LinnSequencer.jpg, skew simulated by adjusting the transformation matrix) - skew-encrypted.pdf (skew.pdf with encryption - access supported by PyPDF2) diff --git a/tests/resources/rotated_skew.pdf b/tests/resources/rotated_skew.pdf new file mode 100644 index 00000000..727a44cf Binary files /dev/null and b/tests/resources/rotated_skew.pdf differ diff --git a/tests/test_main.py b/tests/test_main.py index 0f0771dc..e528a74a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -688,3 +688,36 @@ def test_linearized_pdf_and_indirect_object(spoof_tesseract_noop): check_ocrmypdf( 'epson.pdf', 'test_epson.pdf', env=spoof_tesseract_noop) + + +def test_rotated_skew_timeout(): + """This document contains an image that is rotated 90 into place with a + /Rotate tag and intentionally skewed by altering the transformation matrix. + + This tests for a bug where the combinatino of preprocessing and a tesseract + timeout produced a page whose dimensions did not match the original's. + """ + + input_file = _infile('rotated_skew.pdf') + in_pageinfo = pdf_get_all_pageinfo(input_file)[0] + + assert in_pageinfo['height_pixels'] < in_pageinfo['width_pixels'], \ + "Expected the input page to be landscape" + assert in_pageinfo['rotate'] == 90, "Expected a rotated page" + + out = check_ocrmypdf( + 'rotated_skew.pdf', 'test_rotated_skew.pdf', + '--deskew', '--tesseract-timeout', '0') + + out_pageinfo = pdf_get_all_pageinfo(out)[0] + + assert out_pageinfo['height_pixels'] > out_pageinfo['width_pixels'], \ + "Expected the output page to be portrait" + + assert out_pageinfo['rotate'] == 0, \ + "Expected no page rotation for output" + + assert in_pageinfo['width_pixels'] == out_pageinfo['height_pixels'] and \ + in_pageinfo['height_pixels'] == out_pageinfo['width_pixels'], \ + "Expected page rotation to be baked in" +