@@ -647,7 +647,9 @@ def decodeStreamData(stream: Any) -> Union[str, bytes]: # deprecated
647647
648648
649649def _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 ,
651653) -> mode_str_type :
652654 """Returns the image mode not taking into account mask(transparency)"""
653655 if isinstance (color_space , str ):
@@ -663,26 +665,29 @@ def _get_imagemode(
663665 color_components = cast (int , icc_profile ["/N" ])
664666 color_space = icc_profile .get ("/Alternate" , "" )
665667 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
672675 elif color_space [0 ] == "/Separation" :
673676 color_space = color_space [2 ]
674677 elif color_space [0 ] == "/DeviceN" :
675- color_space = color_space [2 ]
676678 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 ()
677682
678683 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]
680689 "2bit" : "2bits" , # 2 bits images
681690 "4bit" : "4bits" , # 4 bits
682- "/DeviceGray" : "L" ,
683- "palette" : "P" , # reserved for color_components alignment
684- "/DeviceRGB" : "RGB" ,
685- "/DeviceCMYK" : "CMYK" ,
686691 }
687692 mode : mode_str_type = (
688693 mode_map .get (color_space ) # type: ignore
@@ -913,8 +918,11 @@ def _handle_jpx(
913918 # CMYK image without decode requires reverting scale (cf p243,2§ last sentence)
914919 decode = x_object_obj .get (
915920 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+ )
918926 else None ,
919927 )
920928 if (
0 commit comments