Skip to content

Commit a548ca1

Browse files
BUG: Consider BlackIs1 parameter for CCITTFaxDecode filter (#3196)
Closes #3193.
1 parent 873946a commit a548ca1

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

docs/user/cropping-and-transforming.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ writer.write("out-pg-transform.pdf")
179179

180180
### Scaling the page only
181181

182-
To scale the page by `sx` in the X direction and `sy` in the Y direction:
182+
To scale the page by `sx` in the X direction and `sy` in the Y direction:
183183

184184
```python
185185
from pypdf.generic import RectangleObject

pypdf/filters.py

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from .errors import DeprecationError, PdfReadError, PdfStreamError
6060
from .generic import (
6161
ArrayObject,
62+
BooleanObject,
6263
DictionaryObject,
6364
IndirectObject,
6465
NullObject,
@@ -755,6 +756,11 @@ def _apply_alpha(
755756
# Get filters
756757
filters = x_object_obj.get(SA.FILTER, NullObject()).get_object()
757758
lfilters = filters[-1] if isinstance(filters, list) else filters
759+
decode_parms = x_object_obj.get(SA.DECODE_PARMS, None)
760+
if isinstance(decode_parms, (tuple, list)):
761+
decode_parms = decode_parms[0]
762+
else:
763+
decode_parms = {}
758764

759765
extension = None
760766
if lfilters in (FT.FLATE_DECODE, FT.RUN_LENGTH_DECODE):
@@ -816,6 +822,10 @@ def _apply_alpha(
816822
img, x_object_obj, obj_as_text, image_format, extension
817823
)
818824

825+
if lfilters == FT.CCITT_FAX_DECODE and decode_parms.get("/BlackIs1", BooleanObject(False)).value is True:
826+
from PIL import ImageOps
827+
img = ImageOps.invert(img)
828+
819829
# Save image to bytes
820830
img_byte_arr = BytesIO()
821831
try:

tests/test_filters.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pathlib import Path
99

1010
import pytest
11-
from PIL import Image
11+
from PIL import Image, ImageOps
1212

1313
from pypdf import PdfReader
1414
from pypdf.errors import DeprecationError, PdfReadError
@@ -642,3 +642,17 @@ def test_ascii85decode__non_recoverable(caplog):
642642
with pytest.raises(ValueError, match="Non-Ascii85 digit found: Ã"):
643643
ASCII85Decode.decode(data)
644644
assert caplog.text == ""
645+
646+
647+
@pytest.mark.enable_socket
648+
def test_ccitt_fax_decode__black_is_1():
649+
url = "https://github.com/user-attachments/files/19288881/imagemagick-CCITTFaxDecode_BlackIs1-true.pdf"
650+
name = "issue3193.pdf"
651+
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
652+
other_reader = PdfReader(RESOURCE_ROOT / "imagemagick-CCITTFaxDecode.pdf")
653+
654+
actual_image = reader.pages[0].images[0].image
655+
expected_image_inverted = other_reader.pages[0].images[0].image
656+
expected_pixels = list(ImageOps.invert(expected_image_inverted).getdata())
657+
actual_pixels = list(actual_image.getdata())
658+
assert expected_pixels == actual_pixels

0 commit comments

Comments
 (0)