Skip to content

Commit ee847b1

Browse files
author
Nathanaël Renaud
committed
refactored code
1 parent 0fc10d7 commit ee847b1

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

pypdf/_page.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ def _get_image(
509509
try:
510510
if isinstance(id, str) and id.find(RES.PATTERN) == 0:
511511
pattern_name = id[len(RES.PATTERN) : id.find("/", len(RES.PATTERN) + 1)]
512-
image_name = id[id.rfind("/") :]
513512

514513
patterns = cast(
515514
DictionaryObject,
@@ -538,26 +537,24 @@ def _get_image(
538537
if self.inline_images is None: # pragma: no cover
539538
raise KeyError("no inline image can be found")
540539
return self.inline_images[id]
541-
elif id.find("/Pattern") == 0:
542-
imgd = _xobj_to_image(cast(DictionaryObject, xobjs[image_name]))
543-
extension, byte_stream = imgd[:2]
544-
f = ImageFile(
545-
name=f"{pattern_name[1:]}_{image_name[1:]}{extension}",
546-
data=byte_stream,
547-
image=imgd[2],
548-
indirect_reference=xobjs[image_name].indirect_reference,
549-
)
550-
return f
540+
541+
if id.find("/Pattern") == 0:
542+
image_identifier = id[id.rfind("/") :]
543+
544+
image_name = pattern_name[1:] + "_" + image_identifier[1:]
551545
else:
552-
imgd = _xobj_to_image(cast(DictionaryObject, xobjs[id]))
553-
extension, byte_stream = imgd[:2]
554-
f = ImageFile(
555-
name=f"{id[1:]}{extension}",
556-
data=byte_stream,
557-
image=imgd[2],
558-
indirect_reference=xobjs[id].indirect_reference,
559-
)
560-
return f
546+
image_identifier = str(id)
547+
image_name = id[1:]
548+
549+
imgd = _xobj_to_image(cast(DictionaryObject, xobjs[image_identifier]))
550+
image_extension, byte_stream = imgd[:2]
551+
552+
return ImageFile(
553+
name=image_name + str(image_extension),
554+
data=byte_stream,
555+
image=imgd[2],
556+
indirect_reference=xobjs[image_identifier].indirect_reference,
557+
)
561558
else: # in a sub object
562559
ids = id[1:]
563560
return self._get_image(ids, cast(DictionaryObject, xobjs[id[0]]))

0 commit comments

Comments
 (0)