@@ -457,19 +457,35 @@ def _get_ids_image(
457
457
if ancest is None :
458
458
ancest = []
459
459
lst : List [Union [str , List [str ]]] = []
460
- if PG .RESOURCES not in obj or RES .XOBJECT not in cast (
461
- DictionaryObject , obj [PG .RESOURCES ]
462
- ):
463
- return self .inline_images_keys
464
-
465
- x_object = obj [PG .RESOURCES ][RES .XOBJECT ].get_object () # type: ignore
466
- for o in x_object :
467
- if not isinstance (x_object [o ], StreamObject ):
468
- continue
469
- if x_object [o ][IA .SUBTYPE ] == "/Image" :
470
- lst .append (o if len (ancest ) == 0 else ancest + [o ])
471
- else : # is a form with possible images inside
472
- lst .extend (self ._get_ids_image (x_object [o ], ancest + [o ], call_stack ))
460
+
461
+ if PG .RESOURCES in obj :
462
+ if RES .PATTERN in cast (DictionaryObject , obj [PG .RESOURCES ]):
463
+ for patternName , pattern in obj [PG .RESOURCES ][RES .PATTERN ].items ():
464
+ if PG .RESOURCES in pattern .get_object ():
465
+ if RES .XOBJECT in cast (DictionaryObject , pattern [PG .RESOURCES ]):
466
+ x_object = pattern [PG .RESOURCES ][RES .XOBJECT ].get_object () # type: ignore
467
+ for o in x_object :
468
+ if not isinstance (x_object [o ], StreamObject ):
469
+ continue
470
+ if x_object [o ][IA .SUBTYPE ] == "/Image" :
471
+ lst .append (
472
+ f"{ RES .PATTERN } { patternName } { o } "
473
+ if len (ancest ) == 0
474
+ else ancest + [o ]
475
+ )
476
+
477
+ if RES .XOBJECT in cast (DictionaryObject , obj [PG .RESOURCES ]):
478
+ x_object = obj [PG .RESOURCES ][RES .XOBJECT ].get_object () # type: ignore
479
+ for o in x_object :
480
+ if not isinstance (x_object [o ], StreamObject ):
481
+ continue
482
+ if x_object [o ][IA .SUBTYPE ] == "/Image" :
483
+ lst .append (o if len (ancest ) == 0 else ancest + [o ])
484
+ else : # is a form with possible images inside
485
+ lst .extend (
486
+ self ._get_ids_image (x_object [o ], ancest + [o ], call_stack )
487
+ )
488
+
473
489
return lst + self .inline_images_keys
474
490
475
491
def _get_image (
@@ -484,9 +500,27 @@ def _get_image(
484
500
if isinstance (id , List ) and len (id ) == 1 :
485
501
id = id [0 ]
486
502
try :
487
- xobjs = cast (
488
- DictionaryObject , cast (DictionaryObject , obj [PG .RESOURCES ])[RES .XOBJECT ]
489
- )
503
+ if isinstance (id , str ) and id .find (RES .PATTERN ) == 0 :
504
+ pattern_name = id [len (RES .PATTERN ) : id .find ("/" , len (RES .PATTERN ) + 1 )]
505
+ image_name = id [id .rfind ("/" ) :]
506
+
507
+ patterns = cast (
508
+ DictionaryObject ,
509
+ cast (DictionaryObject , obj [PG .RESOURCES ])[RES .PATTERN ],
510
+ )
511
+
512
+ xobjs = cast (
513
+ DictionaryObject ,
514
+ cast (DictionaryObject , patterns [pattern_name ][PG .RESOURCES ])[
515
+ RES .XOBJECT
516
+ ],
517
+ )
518
+
519
+ else :
520
+ xobjs = cast (
521
+ DictionaryObject ,
522
+ cast (DictionaryObject , obj [PG .RESOURCES ])[RES .XOBJECT ],
523
+ )
490
524
except KeyError :
491
525
if not (id [0 ] == "~" and id [- 1 ] == "~" ):
492
526
raise
@@ -497,16 +531,26 @@ def _get_image(
497
531
if self .inline_images is None : # pragma: no cover
498
532
raise KeyError ("no inline image can be found" )
499
533
return self .inline_images [id ]
500
-
501
- imgd = _xobj_to_image (cast (DictionaryObject , xobjs [id ]))
502
- extension , byte_stream = imgd [:2 ]
503
- f = ImageFile (
504
- name = f"{ id [1 :]} { extension } " ,
505
- data = byte_stream ,
506
- image = imgd [2 ],
507
- indirect_reference = xobjs [id ].indirect_reference ,
508
- )
509
- return f
534
+ elif id .find ("/Pattern" ) == 0 :
535
+ imgd = _xobj_to_image (cast (DictionaryObject , xobjs [image_name ]))
536
+ extension , byte_stream = imgd [:2 ]
537
+ f = ImageFile (
538
+ name = f"{ pattern_name [1 :]} _{ image_name [1 :]} { extension } " ,
539
+ data = byte_stream ,
540
+ image = imgd [2 ],
541
+ indirect_reference = xobjs [image_name ].indirect_reference ,
542
+ )
543
+ return f
544
+ else :
545
+ imgd = _xobj_to_image (cast (DictionaryObject , xobjs [id ]))
546
+ extension , byte_stream = imgd [:2 ]
547
+ f = ImageFile (
548
+ name = f"{ id [1 :]} { extension } " ,
549
+ data = byte_stream ,
550
+ image = imgd [2 ],
551
+ indirect_reference = xobjs [id ].indirect_reference ,
552
+ )
553
+ return f
510
554
else : # in a sub object
511
555
ids = id [1 :]
512
556
return self ._get_image (ids , cast (DictionaryObject , xobjs [id [0 ]]))
0 commit comments