@@ -647,7 +647,9 @@ def decodeStreamData(stream: Any) -> Union[str, bytes]: # deprecated
647
647
648
648
649
649
def _get_imagemode (
650
- color_space : Union [str , List [Any ]], color_components : int , prev_mode : mode_str_type
650
+ color_space : Union [str , List [Any ], Any ],
651
+ color_components : int ,
652
+ prev_mode : mode_str_type ,
651
653
) -> mode_str_type :
652
654
"""Returns the image mode not taking into account mask(transparency)"""
653
655
if isinstance (color_space , str ):
@@ -663,26 +665,29 @@ def _get_imagemode(
663
665
color_components = cast (int , icc_profile ["/N" ])
664
666
color_space = icc_profile .get ("/Alternate" , "" )
665
667
elif color_space [0 ] == "/Indexed" :
666
- color_space = color_space [1 ].get_object ()
667
- if isinstance (color_space , list ):
668
- color_space = color_space [1 ].get_object ().get ("/Alternate" , "" )
669
- color_components = 1 if "Gray" in color_space else 2
670
- if not (isinstance (color_space , str ) and "Gray" in color_space ):
671
- color_space = "palette"
668
+ color_space = color_space [1 ]
669
+ if isinstance (color_space , IndirectObject ):
670
+ color_space = color_space .get_object ()
671
+ mode2 = _get_imagemode (color_space , color_components , prev_mode )
672
+ if mode2 in ("RGB" , "CMYK" ):
673
+ mode2 = "P"
674
+ return mode2
672
675
elif color_space [0 ] == "/Separation" :
673
676
color_space = color_space [2 ]
674
677
elif color_space [0 ] == "/DeviceN" :
675
- color_space = color_space [2 ]
676
678
color_components = len (color_space [1 ])
679
+ color_space = color_space [2 ]
680
+ if isinstance (color_space , IndirectObject ): # pragma: no cover
681
+ color_space = color_space .get_object ()
677
682
678
683
mode_map = {
679
- "1bit" : "1" , # 0 will be used for 1 bit
684
+ "1bit" : "1" , # pos [0] will be used for 1 bit
685
+ "/DeviceGray" : "L" , # must be in pos [1]
686
+ "palette" : "P" , # must be in pos [2] for color_components align.
687
+ "/DeviceRGB" : "RGB" , # must be in pos [3]
688
+ "/DeviceCMYK" : "CMYK" , # must be in pos [4]
680
689
"2bit" : "2bits" , # 2 bits images
681
690
"4bit" : "4bits" , # 4 bits
682
- "/DeviceGray" : "L" ,
683
- "palette" : "P" , # reserved for color_components alignment
684
- "/DeviceRGB" : "RGB" ,
685
- "/DeviceCMYK" : "CMYK" ,
686
691
}
687
692
mode : mode_str_type = (
688
693
mode_map .get (color_space ) # type: ignore
@@ -913,8 +918,11 @@ def _handle_jpx(
913
918
# CMYK image without decode requires reverting scale (cf p243,2§ last sentence)
914
919
decode = x_object_obj .get (
915
920
IA .DECODE ,
916
- ([1.0 , 0.0 ] * 4 )
917
- if img .mode == "CMYK" and lfilters in (FT .DCT_DECODE , FT .JPX_DECODE )
921
+ ([1.0 , 0.0 ] * len (img .getbands ()))
922
+ if (
923
+ (img .mode == "CMYK" or (mode == "CMYK" and img .mode == "L" ))
924
+ and lfilters in (FT .DCT_DECODE , FT .JPX_DECODE )
925
+ )
918
926
else None ,
919
927
)
920
928
if (
0 commit comments