From 7c0940609a1526ef4d6d6357ca343b4aa742bb59 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 8 Feb 2016 12:32:39 -0800 Subject: [PATCH] Take a stab at writing test case for autorotate --- ocrmypdf/ghostscript.py | 5 ++++- test_requirements.txt | 2 +- tests/test_main.py | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ocrmypdf/ghostscript.py b/ocrmypdf/ghostscript.py index 67e4a9b8..0ab18504 100644 --- a/ocrmypdf/ghostscript.py +++ b/ocrmypdf/ghostscript.py @@ -7,7 +7,8 @@ from shutil import copy from . import get_program -def rasterize_pdf(input_file, output_file, xres, yres, raster_device, log): +def rasterize_pdf(input_file, output_file, xres, yres, raster_device, log, + pageno=1): with NamedTemporaryFile(delete=True) as tmp: args_gs = [ get_program('gs'), @@ -15,6 +16,8 @@ def rasterize_pdf(input_file, output_file, xres, yres, raster_device, log): '-dBATCH', '-dNOPAUSE', '-sDEVICE=%s' % raster_device, + '-dFirstPage=%i' % pageno, + '-dLastPage=%i' % pageno, '-o', tmp.name, '-r{0}x{1}'.format(str(xres), str(yres)), input_file diff --git a/test_requirements.txt b/test_requirements.txt index 7c419275..7a13b208 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -1 +1 @@ -pytest>=2.7.2 \ No newline at end of file +pytest>=2.7.2 diff --git a/tests/test_main.py b/tests/test_main.py index 1ec525ab..3a6ec55e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -238,6 +238,28 @@ def test_argsfile(spoof_tesseract_noop): env=spoof_tesseract_noop) +@pytest.mark.parametrize('renderer', [ + 'hocr', + 'tesseract', + ]) +def test_autorotate(spoof_tesseract_cache, renderer): + import ocrmypdf.ghostscript as ghostscript + import logging + + gslog = logging.getLogger() + + out = check_ocrmypdf('cardinal.pdf', 'test_autorotate_%s.pdf' % renderer, + '-r', '-v', '1') + for n in range(1, 4+1): + ghostscript.rasterize_pdf( + out, _make_output('cardinal-%i.png' % n), xres=100, yres=100, + raster_device='pngmono', log=gslog, pageno=n) + + ghostscript.rasterize_pdf( + _make_input('cardinal.pdf'), _make_output('reference.png'), + xres=100, yres=100, raster_device='pngmono', log=gslog, pageno=1) + + @pytest.mark.parametrize('renderer', [ 'hocr', 'tesseract',