@@ -3251,19 +3251,9 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
3251
3251
transferred. This means that P and PA mode images will lose their palette.
3252
3252
3253
3253
:param obj: Object with array interface
3254
- :param mode: Optional mode to use when reading ``obj``. Will be determined from
3255
- type if ``None``. Deprecated.
3256
-
3257
- This will not be used to convert the data after reading, but will be used to
3258
- change how the data is read::
3259
-
3260
- from PIL import Image
3261
- import numpy as np
3262
- a = np.full((1, 1), 300)
3263
- im = Image.fromarray(a, mode="L")
3264
- im.getpixel((0, 0)) # 44
3265
- im = Image.fromarray(a, mode="RGB")
3266
- im.getpixel((0, 0)) # (44, 1, 0)
3254
+ :param mode: Optional mode to use when reading ``obj``. Since pixel values do not
3255
+ contain information about palettes or color spaces, this can be used to place
3256
+ grayscale L mode data within a P mode image, or read RGB data as YCbCr.
3267
3257
3268
3258
See: :ref:`concept-modes` for general information about modes.
3269
3259
:returns: An image object.
@@ -3274,21 +3264,28 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
3274
3264
shape = arr ["shape" ]
3275
3265
ndim = len (shape )
3276
3266
strides = arr .get ("strides" , None )
3277
- if mode is None :
3278
- try :
3279
- typekey = (1 , 1 ) + shape [2 :], arr ["typestr" ]
3280
- except KeyError as e :
3267
+ try :
3268
+ typekey = (1 , 1 ) + shape [2 :], arr ["typestr" ]
3269
+ except KeyError as e :
3270
+ if mode is not None :
3271
+ typekey = None
3272
+ color_modes = []
3273
+ else :
3281
3274
msg = "Cannot handle this data type"
3282
3275
raise TypeError (msg ) from e
3276
+ if typekey is not None :
3283
3277
try :
3284
- mode , rawmode = _fromarray_typemap [typekey ]
3278
+ typemode , rawmode , color_modes = _fromarray_typemap [typekey ]
3285
3279
except KeyError as e :
3286
3280
typekey_shape , typestr = typekey
3287
3281
msg = f"Cannot handle this data type: { typekey_shape } , { typestr } "
3288
3282
raise TypeError (msg ) from e
3289
- else :
3290
- deprecate ("'mode' parameter" , 13 )
3283
+ if mode is not None :
3284
+ if mode != typemode and mode not in color_modes :
3285
+ deprecate ("'mode' parameter for changing data types" , 13 )
3291
3286
rawmode = mode
3287
+ else :
3288
+ mode = typemode
3292
3289
if mode in ["1" , "L" , "I" , "P" , "F" ]:
3293
3290
ndmax = 2
3294
3291
elif mode == "RGB" :
@@ -3385,29 +3382,29 @@ def fromqpixmap(im: ImageQt.QPixmap) -> ImageFile.ImageFile:
3385
3382
3386
3383
3387
3384
_fromarray_typemap = {
3388
- # (shape, typestr) => mode, rawmode
3385
+ # (shape, typestr) => mode, rawmode, color modes
3389
3386
# first two members of shape are set to one
3390
- ((1 , 1 ), "|b1" ): ("1" , "1;8" ),
3391
- ((1 , 1 ), "|u1" ): ("L" , "L" ),
3392
- ((1 , 1 ), "|i1" ): ("I" , "I;8" ),
3393
- ((1 , 1 ), "<u2" ): ("I" , "I;16" ),
3394
- ((1 , 1 ), ">u2" ): ("I" , "I;16B" ),
3395
- ((1 , 1 ), "<i2" ): ("I" , "I;16S" ),
3396
- ((1 , 1 ), ">i2" ): ("I" , "I;16BS" ),
3397
- ((1 , 1 ), "<u4" ): ("I" , "I;32" ),
3398
- ((1 , 1 ), ">u4" ): ("I" , "I;32B" ),
3399
- ((1 , 1 ), "<i4" ): ("I" , "I;32S" ),
3400
- ((1 , 1 ), ">i4" ): ("I" , "I;32BS" ),
3401
- ((1 , 1 ), "<f4" ): ("F" , "F;32F" ),
3402
- ((1 , 1 ), ">f4" ): ("F" , "F;32BF" ),
3403
- ((1 , 1 ), "<f8" ): ("F" , "F;64F" ),
3404
- ((1 , 1 ), ">f8" ): ("F" , "F;64BF" ),
3405
- ((1 , 1 , 2 ), "|u1" ): ("LA" , "LA" ),
3406
- ((1 , 1 , 3 ), "|u1" ): ("RGB" , "RGB" ),
3407
- ((1 , 1 , 4 ), "|u1" ): ("RGBA" , "RGBA" ),
3387
+ ((1 , 1 ), "|b1" ): ("1" , "1;8" , [] ),
3388
+ ((1 , 1 ), "|u1" ): ("L" , "L" , [ "P" ] ),
3389
+ ((1 , 1 ), "|i1" ): ("I" , "I;8" , [] ),
3390
+ ((1 , 1 ), "<u2" ): ("I" , "I;16" , [] ),
3391
+ ((1 , 1 ), ">u2" ): ("I" , "I;16B" , [] ),
3392
+ ((1 , 1 ), "<i2" ): ("I" , "I;16S" , [] ),
3393
+ ((1 , 1 ), ">i2" ): ("I" , "I;16BS" , [] ),
3394
+ ((1 , 1 ), "<u4" ): ("I" , "I;32" , [] ),
3395
+ ((1 , 1 ), ">u4" ): ("I" , "I;32B" , [] ),
3396
+ ((1 , 1 ), "<i4" ): ("I" , "I;32S" , [] ),
3397
+ ((1 , 1 ), ">i4" ): ("I" , "I;32BS" , [] ),
3398
+ ((1 , 1 ), "<f4" ): ("F" , "F;32F" , [] ),
3399
+ ((1 , 1 ), ">f4" ): ("F" , "F;32BF" , [] ),
3400
+ ((1 , 1 ), "<f8" ): ("F" , "F;64F" , [] ),
3401
+ ((1 , 1 ), ">f8" ): ("F" , "F;64BF" , [] ),
3402
+ ((1 , 1 , 2 ), "|u1" ): ("LA" , "LA" , [ "La" , "PA" ] ),
3403
+ ((1 , 1 , 3 ), "|u1" ): ("RGB" , "RGB" , [ "YCbCr" , "LAB" , "HSV" ] ),
3404
+ ((1 , 1 , 4 ), "|u1" ): ("RGBA" , "RGBA" , [ "RGBa" ] ),
3408
3405
# shortcuts:
3409
- ((1 , 1 ), f"{ _ENDIAN } i4" ): ("I" , "I" ),
3410
- ((1 , 1 ), f"{ _ENDIAN } f4" ): ("F" , "F" ),
3406
+ ((1 , 1 ), f"{ _ENDIAN } i4" ): ("I" , "I" , [] ),
3407
+ ((1 , 1 ), f"{ _ENDIAN } f4" ): ("F" , "F" , [] ),
3411
3408
}
3412
3409
3413
3410
0 commit comments