From 41ffc2c5ac3b12aa7fa982163600b50062af7184 Mon Sep 17 00:00:00 2001 From: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:11:46 +0200 Subject: [PATCH] BUG: images property fails if NullObject in list (#2215) Discovered while working on #2214 --- pypdf/_page.py | 3 +++ tests/test_images.py | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pypdf/_page.py b/pypdf/_page.py index 32720992a..520d71653 100644 --- a/pypdf/_page.py +++ b/pypdf/_page.py @@ -83,6 +83,7 @@ NullObject, NumberObject, RectangleObject, + StreamObject, ) MERGE_CROP_BOX = "cropbox" # pypdf<=3.4.0 used 'trimbox' @@ -509,6 +510,8 @@ def _get_ids_image( x_object = obj[PG.RESOURCES][RES.XOBJECT].get_object() # type: ignore for o in x_object: + if not isinstance(x_object[o], StreamObject): + continue if x_object[o][IA.SUBTYPE] == "/Image": lst.append(o if len(ancest) == 0 else ancest + [o]) else: # is a form with possible images inside diff --git a/tests/test_images.py b/tests/test_images.py index 2f14c7b38..9e03c9f35 100644 --- a/tests/test_images.py +++ b/tests/test_images.py @@ -12,8 +12,8 @@ import pytest from PIL import Image, ImageChops, ImageDraw -from pypdf import PdfReader -from pypdf._page import PageObject +from pypdf import PageObject, PdfReader +from pypdf.generic import NameObject, NullObject from . import get_data_from_url @@ -218,4 +218,5 @@ def test_loop_in_image_keys(): url = "https://github.com/py-pdf/pypdf/files/12309492/example_134.pdf" name = "iss2077.pdf" reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) + reader.pages[0]["/Resources"]["/XObject"][NameObject("/toto")] = NullObject() reader.pages[0].images.keys()