Skip to content

Commit e9241ac

Browse files
BUG: Correctly handle image mode 1 with FlateDecode (#2249)
Fixes #2248
1 parent faa8c68 commit e9241ac

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pypdf/_xobj_image_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ def bits2byte(data: bytes, size: Tuple[int, int], bits: int) -> bytes:
186186
lookup = None
187187
else:
188188
if img.mode == "1":
189-
colors_arr = [lookup[x - nb : x] for x in range(nb, len(lookup), nb)]
189+
# Two values ("high" and "low").
190+
assert len(lookup) == 2 * nb, len(lookup)
191+
colors_arr = [lookup[:nb], lookup[nb:]]
190192
arr = b"".join(
191193
[
192194
b"".join(

tests/test_filters.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pypdf.generic import ArrayObject, DictionaryObject, NameObject, NumberObject
2323

2424
from . import get_data_from_url
25+
from .test_encryption import HAS_AES
2526
from .test_images import image_similarity
2627

2728
filter_inputs = (
@@ -637,3 +638,14 @@ def test_nested_device_n_color_space():
637638
name = "issue2240.pdf"
638639
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
639640
reader.pages[0].images[0]
641+
642+
643+
@pytest.mark.enable_socket()
644+
@pytest.mark.skipif(not HAS_AES, reason="No AES implementation")
645+
def test_flate_decode_with_image_mode_1():
646+
"""From #2248"""
647+
url = "https://github.com/py-pdf/pypdf/files/12847339/Prototype-Declaration-VDE4110-HYD-5000-20000-ZSS-DE.pdf"
648+
name = "issue2248.pdf"
649+
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
650+
for image in reader.pages[7].images:
651+
_ = image

0 commit comments

Comments
 (0)