diff --git a/src/ocrmypdf/_exec/unpaper.py b/src/ocrmypdf/_exec/unpaper.py index 479959ef..aad2a9b4 100644 --- a/src/ocrmypdf/_exec/unpaper.py +++ b/src/ocrmypdf/_exec/unpaper.py @@ -72,13 +72,13 @@ def version() -> str: return get_version('unpaper') -SUFFIXES = {'1': '.pbm', 'L': '.pgm', 'RGB': '.ppm'} +SUPPORTED_MODES = {'1', 'L', 'RGB'} -def _convert_image(im: Image.Image) -> Tuple[Image.Image, bool, str]: +def _convert_image(im: Image.Image) -> Tuple[Image.Image, bool]: im_modified = False - if im.mode not in SUFFIXES: + if im.mode not in SUPPORTED_MODES: log.info("Converting image to other colorspace") try: if im.mode == 'P' and len(im.getcolors()) == 2: @@ -91,13 +91,11 @@ def _convert_image(im: Image.Image) -> Tuple[Image.Image, bool, str]: ) from e else: im_modified = True - try: - suffix = SUFFIXES[im.mode] - except KeyError: - raise MissingDependencyError( - "Failed to convert image to a supported format." - ) from None - return im, im_modified, suffix + if im.mode not in SUPPORTED_MODES: + raise MissingDependencyError( + "Failed to convert image to a supported format." + ) from None + return im, im_modified @contextmanager @@ -105,19 +103,21 @@ def _setup_unpaper_io(input_file: Path) -> Iterator[Tuple[Path, Path, Path]]: with Image.open(input_file) as im: if im.width * im.height >= UNPAPER_IMAGE_PIXEL_LIMIT: raise UnpaperImageTooLargeError(w=im.width, h=im.height) - im, im_modified, suffix = _convert_image(im) + im, im_modified = _convert_image(im) with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: tmppath = Path(tmpdir) - if im_modified or input_file.suffix != '.pnm': - input_pnm = tmppath / 'input.pnm' - im.save(input_pnm, format='PPM') + if im_modified or input_file.suffix != '.png': + input_png = tmppath / 'input.png' + im.save(input_png, format='PNG') else: # No changes, PNG input, just use the file we already have - input_pnm = input_file + input_png = input_file - output_pnm = tmppath / f'output{suffix}' - yield input_pnm, output_pnm, tmppath + # unpaper can write .png too, but it seems to write them slowly + # adds a few seconds to test suite - so just use pnm + output_pnm = tmppath / 'output.pnm' + yield input_png, output_pnm, tmppath def run_unpaper( @@ -125,7 +125,7 @@ def run_unpaper( ) -> None: args_unpaper = ['unpaper', '-v', '--dpi', str(round(dpi, 6))] + mode_args - with _setup_unpaper_io(input_file) as (input_pnm, output_pnm, tmpdir): + with _setup_unpaper_io(input_file) as (input_png, output_pnm, tmpdir): # To prevent any shenanigans from accepting arbitrary parameters in # --unpaper-args, we: # 1) run with cwd set to a tmpdir with only unpaper's files @@ -133,7 +133,7 @@ def run_unpaper( # 3) append absolute paths for the input and output file # This should ensure that a user cannot clobber some other file with # their unpaper arguments (whether intentionally or otherwise) - args_unpaper.extend([os.fspath(input_pnm), os.fspath(output_pnm)]) + args_unpaper.extend([os.fspath(input_png), os.fspath(output_pnm)]) run( args_unpaper, close_fds=True, diff --git a/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/pdf.bin b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/pdf.bin new file mode 100644 index 00000000..937ee885 Binary files /dev/null and b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/pdf.bin differ diff --git a/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/stderr.bin b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/stderr.bin new file mode 100644 index 00000000..16b617e5 --- /dev/null +++ b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/stderr.bin @@ -0,0 +1 @@ +Tesseract Open Source OCR Engine v4.1.1 with Leptonica diff --git a/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/stdout.bin b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/stdout.bin new file mode 100644 index 00000000..e69de29b diff --git a/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/txt.bin b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/txt.bin new file mode 100644 index 00000000..25fdded2 --- /dev/null +++ b/tests/cache/francais/__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt/txt.bin @@ -0,0 +1,13 @@ +Portez ce vieux whisky au juge +blond qui fume sur son Ile +interieure, a cöte de l'alcöve +ovoide, oU les büches se +consument dans l'ätre, ce qui +lui permet de penser & la +caenogenese de |'etre dont il +est question dans la cause +ambigu& entendue a MoY, dans +un capharnaüm qui, pense-t-il, +diminue ca et la la qualite de son +ceuvre. + \ No newline at end of file diff --git a/tests/cache/manifest.jsonl b/tests/cache/manifest.jsonl index fdd1affe..995ec062 100644 --- a/tests/cache/manifest.jsonl +++ b/tests/cache/manifest.jsonl @@ -82,3 +82,4 @@ {"tesseract_version": "4.1.1", "system": "Linux", "python": "3.9.5", "argv_slug": "__-l__eng__--oem__1__000001_ocr.png__000001_ocr_tess__pdf__txt", "sourcefile": "resources/trivial.pdf", "args": ["-l", "eng", "--oem", "1", "-c", "textonly_pdf=1", "$TMPDIR/000001_ocr.png", "$TMPDIR/000001_ocr_tess", "pdf", "txt"]} {"tesseract_version": "5.0.0", "system": "Linux", "python": "3.9.5", "argv_slug": "__-l__eng__thresholding_method=1__000001_ocr.png__000001_ocr_tess__pdf__txt", "sourcefile": "resources/trivial.pdf", "args": ["-l", "eng", "-c", "textonly_pdf=1", "-c", "thresholding_method=1", "$TMPDIR/000001_ocr.png", "$TMPDIR/000001_ocr_tess", "pdf", "txt"]} {"tesseract_version": "5.0.0", "system": "Linux", "python": "3.9.5", "argv_slug": "__-l__eng__thresholding_method=2__000001_ocr.png__000001_ocr_tess__pdf__txt", "sourcefile": "resources/trivial.pdf", "args": ["-l", "eng", "-c", "textonly_pdf=1", "-c", "thresholding_method=2", "$TMPDIR/000001_ocr.png", "$TMPDIR/000001_ocr_tess", "pdf", "txt"]} +{"tesseract_version": "4.1.1", "system": "Linux", "python": "3.10.4", "argv_slug": "__-l__deu__000001_ocr.png__000001_ocr_tess__pdf__txt", "sourcefile": "resources/francais.pdf", "args": ["-l", "deu", "-c", "textonly_pdf=1", "$TMPDIR/000001_ocr.png", "$TMPDIR/000001_ocr_tess", "pdf", "txt"]}