diff --git a/docs/release_notes.rst b/docs/release_notes.rst index e9ac8b7b..6d8e5ade 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -13,6 +13,11 @@ Note that it is licensed under GPLv3, so scripts that ``import ocrmypdf`` and ar find: [^`]\#([0-9]{1,3})[^0-9] replace: `#$1 `_ +v7.0.1 +------ + +- Fix compatibility with img2pdf >= 0.3.0 by rejecting input images that have an alpha channel + v7.0.0 ------ @@ -61,6 +66,12 @@ v7.0.0 + It may be necessary to separately ``pip install pycparser`` to avoid `another Python 3.7 issue `_. +v6.2.3 +------ + +- Fix compatibility with img2pdf >= 0.3.0 by rejecting input images that have an alpha channel + + v6.2.2 ------ diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index 56062bd5..835866bc 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -19,7 +19,6 @@ from contextlib import suppress from shutil import copyfileobj, copyfile from pathlib import Path from datetime import datetime, timezone - import sys import os import re @@ -85,6 +84,13 @@ def triage_image_file(input_file, output_file, log, options): "image was scanned and specify it using --image-dpi.") raise DpiError() + if im.mode in ('RGBA', 'LA'): + log.error( + "The input image has an alpha channel. Remove the alpha " + "channel first." + ) + raise UnsupportedImageFormatError() + if 'iccprofile' not in im.info: if im.mode == 'RGB': log.info('Input image has no ICC profile, assuming sRGB') diff --git a/tests/resources/README.rst b/tests/resources/README.rst index 786649d4..cc2caa8a 100644 --- a/tests/resources/README.rst +++ b/tests/resources/README.rst @@ -146,8 +146,9 @@ Assemblies These test resources are assemblies or derivatives from other previously mentioned files, released under the same license terms as their input files. -- baiona_gray.png (from baiona.png) -- baiona_colormapped.png (from baiona.png) +- baiona_gray.png (from baiona.png, grayscale version) +- baiona_colormapped.png (from baiona.png, palette version) +- baiona_alpha.png (from baiona.png, RGB+A version) - cardinal.pdf (four cardinal directions, baked-in rotated copies of linn.png) - ccitt.pdf (linn.png, converted to CCITT encoding) - encrypted_algo4.pdf (congress.jpg, encrypted with algorithm 4 - not supported by PyPDF2) diff --git a/tests/resources/baiona_alpha.png b/tests/resources/baiona_alpha.png new file mode 100644 index 00000000..143f48c5 Binary files /dev/null and b/tests/resources/baiona_alpha.png differ diff --git a/tests/test_main.py b/tests/test_main.py index 201e48c4..dea376cf 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -691,6 +691,7 @@ def test_no_contents(spoof_tesseract_noop, resources, outpdf): @pytest.mark.parametrize('image', [ 'baiona.png', 'baiona_gray.png', + 'baiona_alpha.png', 'congress.jpg' ]) def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec, @@ -699,7 +700,6 @@ def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec, output_file = str(outpdf) im = Image.open(input_file) - # Runs: ocrmypdf - output.pdf < testfile with open(input_file, 'rb') as input_stream: p_args = ocrmypdf_exec + [ @@ -710,7 +710,12 @@ def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec, stdin=input_stream, env=spoof_tesseract_noop) out, err = p.communicate() - assert p.returncode == ExitCode.ok + if im.mode in ('RGBA', 'LA'): + # If alpha image is input, expect an error + assert p.returncode != ExitCode.ok and b'alpha' in err + return + + assert p.returncode == ExitCode.ok, err.decode('utf-8') pdfinfo = PdfInfo(output_file) @@ -754,7 +759,7 @@ def test_compression_changed(spoof_tesseract_noop, ocrmypdf_exec, stdin=input_stream, env=spoof_tesseract_noop) out, err = p.communicate() - assert p.returncode == ExitCode.ok + assert p.returncode == ExitCode.ok, err pdfinfo = PdfInfo(output_file)