@@ -3259,19 +3259,10 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
32593259 transferred. This means that P and PA mode images will lose their palette.
32603260
32613261 :param obj: Object with array interface
3262- :param mode: Optional mode to use when reading ``obj``. Will be determined from
3263- type if ``None``. Deprecated.
3264-
3265- This will not be used to convert the data after reading, but will be used to
3266- change how the data is read::
3267-
3268- from PIL import Image
3269- import numpy as np
3270- a = np.full((1, 1), 300)
3271- im = Image.fromarray(a, mode="L")
3272- im.getpixel((0, 0)) # 44
3273- im = Image.fromarray(a, mode="RGB")
3274- im.getpixel((0, 0)) # (44, 1, 0)
3262+ :param mode: Optional mode to use when reading ``obj``. Since pixel values do not
3263+ contain information about palettes or color spaces, this can be used to place
3264+ grayscale L mode data within a P mode image, or read RGB data as YCbCr for
3265+ example.
32753266
32763267 See: :ref:`concept-modes` for general information about modes.
32773268 :returns: An image object.
@@ -3282,21 +3273,28 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
32823273 shape = arr ["shape" ]
32833274 ndim = len (shape )
32843275 strides = arr .get ("strides" , None )
3285- if mode is None :
3286- try :
3287- typekey = (1 , 1 ) + shape [2 :], arr ["typestr" ]
3288- except KeyError as e :
3276+ try :
3277+ typekey = (1 , 1 ) + shape [2 :], arr ["typestr" ]
3278+ except KeyError as e :
3279+ if mode is not None :
3280+ typekey = None
3281+ color_modes : list [str ] = []
3282+ else :
32893283 msg = "Cannot handle this data type"
32903284 raise TypeError (msg ) from e
3285+ if typekey is not None :
32913286 try :
3292- mode , rawmode = _fromarray_typemap [typekey ]
3287+ typemode , rawmode , color_modes = _fromarray_typemap [typekey ]
32933288 except KeyError as e :
32943289 typekey_shape , typestr = typekey
32953290 msg = f"Cannot handle this data type: { typekey_shape } , { typestr } "
32963291 raise TypeError (msg ) from e
3297- else :
3298- deprecate ("'mode' parameter" , 13 )
3292+ if mode is not None :
3293+ if mode != typemode and mode not in color_modes :
3294+ deprecate ("'mode' parameter for changing data types" , 13 )
32993295 rawmode = mode
3296+ else :
3297+ mode = typemode
33003298 if mode in ["1" , "L" , "I" , "P" , "F" ]:
33013299 ndmax = 2
33023300 elif mode == "RGB" :
@@ -3393,29 +3391,29 @@ def fromqpixmap(im: ImageQt.QPixmap) -> ImageFile.ImageFile:
33933391
33943392
33953393_fromarray_typemap = {
3396- # (shape, typestr) => mode, rawmode
3394+ # (shape, typestr) => mode, rawmode, color modes
33973395 # first two members of shape are set to one
3398- ((1 , 1 ), "|b1" ): ("1" , "1;8" ),
3399- ((1 , 1 ), "|u1" ): ("L" , "L" ),
3400- ((1 , 1 ), "|i1" ): ("I" , "I;8" ),
3401- ((1 , 1 ), "<u2" ): ("I" , "I;16" ),
3402- ((1 , 1 ), ">u2" ): ("I" , "I;16B" ),
3403- ((1 , 1 ), "<i2" ): ("I" , "I;16S" ),
3404- ((1 , 1 ), ">i2" ): ("I" , "I;16BS" ),
3405- ((1 , 1 ), "<u4" ): ("I" , "I;32" ),
3406- ((1 , 1 ), ">u4" ): ("I" , "I;32B" ),
3407- ((1 , 1 ), "<i4" ): ("I" , "I;32S" ),
3408- ((1 , 1 ), ">i4" ): ("I" , "I;32BS" ),
3409- ((1 , 1 ), "<f4" ): ("F" , "F;32F" ),
3410- ((1 , 1 ), ">f4" ): ("F" , "F;32BF" ),
3411- ((1 , 1 ), "<f8" ): ("F" , "F;64F" ),
3412- ((1 , 1 ), ">f8" ): ("F" , "F;64BF" ),
3413- ((1 , 1 , 2 ), "|u1" ): ("LA" , "LA" ),
3414- ((1 , 1 , 3 ), "|u1" ): ("RGB" , "RGB" ),
3415- ((1 , 1 , 4 ), "|u1" ): ("RGBA" , "RGBA" ),
3396+ ((1 , 1 ), "|b1" ): ("1" , "1;8" , [] ),
3397+ ((1 , 1 ), "|u1" ): ("L" , "L" , [ "P" ] ),
3398+ ((1 , 1 ), "|i1" ): ("I" , "I;8" , [] ),
3399+ ((1 , 1 ), "<u2" ): ("I" , "I;16" , [] ),
3400+ ((1 , 1 ), ">u2" ): ("I" , "I;16B" , [] ),
3401+ ((1 , 1 ), "<i2" ): ("I" , "I;16S" , [] ),
3402+ ((1 , 1 ), ">i2" ): ("I" , "I;16BS" , [] ),
3403+ ((1 , 1 ), "<u4" ): ("I" , "I;32" , [] ),
3404+ ((1 , 1 ), ">u4" ): ("I" , "I;32B" , [] ),
3405+ ((1 , 1 ), "<i4" ): ("I" , "I;32S" , [] ),
3406+ ((1 , 1 ), ">i4" ): ("I" , "I;32BS" , [] ),
3407+ ((1 , 1 ), "<f4" ): ("F" , "F;32F" , [] ),
3408+ ((1 , 1 ), ">f4" ): ("F" , "F;32BF" , [] ),
3409+ ((1 , 1 ), "<f8" ): ("F" , "F;64F" , [] ),
3410+ ((1 , 1 ), ">f8" ): ("F" , "F;64BF" , [] ),
3411+ ((1 , 1 , 2 ), "|u1" ): ("LA" , "LA" , [ "La" , "PA" ] ),
3412+ ((1 , 1 , 3 ), "|u1" ): ("RGB" , "RGB" , [ "YCbCr" , "LAB" , "HSV" ] ),
3413+ ((1 , 1 , 4 ), "|u1" ): ("RGBA" , "RGBA" , [ "RGBa" , "RGBX" , "CMYK" ] ),
34163414 # shortcuts:
3417- ((1 , 1 ), f"{ _ENDIAN } i4" ): ("I" , "I" ),
3418- ((1 , 1 ), f"{ _ENDIAN } f4" ): ("F" , "F" ),
3415+ ((1 , 1 ), f"{ _ENDIAN } i4" ): ("I" , "I" , [] ),
3416+ ((1 , 1 ), f"{ _ENDIAN } f4" ): ("F" , "F" , [] ),
34193417}
34203418
34213419
0 commit comments