@@ -796,15 +796,15 @@ def decode_stream_data(stream: Any) -> bytes:
796
796
return data
797
797
798
798
799
- def _xobj_to_image (x_object_obj : dict [str , Any ]) -> tuple [Optional [str ], bytes , Any ]:
799
+ def _xobj_to_image (x_object : dict [str , Any ]) -> tuple [Optional [str ], bytes , Any ]:
800
800
"""
801
801
Users need to have the pillow package installed.
802
802
803
803
It's unclear if pypdf will keep this function here, hence it's private.
804
804
It might get removed at any point.
805
805
806
806
Args:
807
- x_object_obj :
807
+ x_object :
808
808
809
809
Returns:
810
810
Tuple[file extension, bytes, PIL.Image.Image]
@@ -822,20 +822,20 @@ def _xobj_to_image(x_object_obj: dict[str, Any]) -> tuple[Optional[str], bytes,
822
822
823
823
def _apply_alpha (
824
824
img : Image .Image ,
825
- x_object_obj : dict [str , Any ],
825
+ x_object : dict [str , Any ],
826
826
obj_as_text : str ,
827
827
image_format : str ,
828
828
extension : str ,
829
829
) -> tuple [Image .Image , str , str ]:
830
830
alpha = None
831
- if IA .S_MASK in x_object_obj : # add alpha channel
832
- alpha = _xobj_to_image (x_object_obj [IA .S_MASK ])[2 ]
831
+ if IA .S_MASK in x_object : # add alpha channel
832
+ alpha = _xobj_to_image (x_object [IA .S_MASK ])[2 ]
833
833
if img .size != alpha .size :
834
834
logger_warning (
835
835
f"image and mask size not matching: { obj_as_text } " , __name__
836
836
)
837
837
else :
838
- # TODO : implement mask
838
+ # TODO: implement mask
839
839
if alpha .mode != "L" :
840
840
alpha = alpha .convert ("L" )
841
841
if img .mode == "P" :
@@ -844,40 +844,40 @@ def _apply_alpha(
844
844
img = img .convert ("L" )
845
845
img .putalpha (alpha )
846
846
if "JPEG" in image_format :
847
- extension = ".jp2"
848
847
image_format = "JPEG2000"
848
+ extension = ".jp2"
849
849
else :
850
- extension = ".png"
851
850
image_format = "PNG"
851
+ extension = ".png"
852
852
return img , extension , image_format
853
853
854
- # for error reporting
854
+ # For error reporting
855
855
obj_as_text = (
856
- x_object_obj .indirect_reference .__repr__ ()
857
- if x_object_obj is None # pragma: no cover
858
- else x_object_obj .__repr__ ()
856
+ x_object .indirect_reference .__repr__ ()
857
+ if x_object is None # pragma: no cover
858
+ else x_object .__repr__ ()
859
859
)
860
860
861
861
# Get size and data
862
- size = (cast (int , x_object_obj [IA .WIDTH ]), cast (int , x_object_obj [IA .HEIGHT ]))
863
- data = x_object_obj .get_data () # type: ignore
862
+ size = (cast (int , x_object [IA .WIDTH ]), cast (int , x_object [IA .HEIGHT ]))
863
+ data = x_object .get_data () # type: ignore
864
864
if isinstance (data , str ): # pragma: no cover
865
865
data = data .encode ()
866
866
if len (data ) % (size [0 ] * size [1 ]) == 1 and data [- 1 ] == 0x0A : # ie. '\n'
867
867
data = data [:- 1 ]
868
868
869
869
# Get color properties
870
- colors = x_object_obj .get ("/Colors" , 1 )
871
- color_space : Any = x_object_obj .get ("/ColorSpace" , NullObject ()).get_object ()
870
+ colors = x_object .get ("/Colors" , 1 )
871
+ color_space : Any = x_object .get ("/ColorSpace" , NullObject ()).get_object ()
872
872
if isinstance (color_space , list ) and len (color_space ) == 1 :
873
873
color_space = color_space [0 ].get_object ()
874
874
875
- mode , invert_color = _get_mode_and_invert_color (x_object_obj , colors , color_space )
875
+ mode , invert_color = _get_mode_and_invert_color (x_object , colors , color_space )
876
876
877
877
# Get filters
878
- filters = x_object_obj .get (SA .FILTER , NullObject ()).get_object ()
878
+ filters = x_object .get (SA .FILTER , NullObject ()).get_object ()
879
879
lfilters = filters [- 1 ] if isinstance (filters , list ) else filters
880
- decode_parms = x_object_obj .get (SA .DECODE_PARMS , None )
880
+ decode_parms = x_object .get (SA .DECODE_PARMS , None )
881
881
if decode_parms and isinstance (decode_parms , (tuple , list )):
882
882
decode_parms = decode_parms [0 ]
883
883
else :
@@ -895,16 +895,16 @@ def _apply_alpha(
895
895
colors ,
896
896
obj_as_text ,
897
897
)
898
- elif lfilters in (FT .LZW_DECODE , FT .ASCII_85_DECODE , FT . CCITT_FAX_DECODE ):
898
+ elif lfilters in (FT .LZW_DECODE , FT .ASCII_85_DECODE ):
899
899
# I'm not sure if the following logic is correct.
900
900
# There might not be any relationship between the filters and the
901
901
# extension
902
- if lfilters in (FT .LZW_DECODE , FT .CCITT_FAX_DECODE ):
903
- extension = ".tiff" # mime_type = "image/tiff"
902
+ if lfilters == FT .LZW_DECODE :
904
903
image_format = "TIFF"
904
+ extension = ".tiff" # mime_type = "image/tiff"
905
905
else :
906
- extension = ".png" # mime_type = "image/png"
907
906
image_format = "PNG"
907
+ extension = ".png" # mime_type = "image/png"
908
908
try :
909
909
img = Image .open (BytesIO (data ), formats = ("TIFF" , "PNG" ))
910
910
except UnidentifiedImageError :
@@ -938,7 +938,7 @@ def _apply_alpha(
938
938
False ,
939
939
)
940
940
elif mode == "" :
941
- raise PdfReadError (f"ColorSpace field not found in { x_object_obj } " )
941
+ raise PdfReadError (f"ColorSpace field not found in { x_object } " )
942
942
else :
943
943
img , image_format , extension , invert_color = (
944
944
_extended_image_frombytes (mode , size , data ),
@@ -947,9 +947,9 @@ def _apply_alpha(
947
947
False ,
948
948
)
949
949
950
- img = _apply_decode (img , x_object_obj , lfilters , color_space , invert_color )
950
+ img = _apply_decode (img , x_object , lfilters , color_space , invert_color )
951
951
img , extension , image_format = _apply_alpha (
952
- img , x_object_obj , obj_as_text , image_format , extension
952
+ img , x_object , obj_as_text , image_format , extension
953
953
)
954
954
955
955
if lfilters == FT .CCITT_FAX_DECODE and decode_parms .get ("/BlackIs1" , BooleanObject (False )).value is True :
0 commit comments