@@ -3259,19 +3259,10 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
3259
3259
transferred. This means that P and PA mode images will lose their palette.
3260
3260
3261
3261
: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.
3275
3266
3276
3267
See: :ref:`concept-modes` for general information about modes.
3277
3268
:returns: An image object.
@@ -3282,21 +3273,28 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
3282
3273
shape = arr ["shape" ]
3283
3274
ndim = len (shape )
3284
3275
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 :
3289
3283
msg = "Cannot handle this data type"
3290
3284
raise TypeError (msg ) from e
3285
+ if typekey is not None :
3291
3286
try :
3292
- mode , rawmode = _fromarray_typemap [typekey ]
3287
+ typemode , rawmode , color_modes = _fromarray_typemap [typekey ]
3293
3288
except KeyError as e :
3294
3289
typekey_shape , typestr = typekey
3295
3290
msg = f"Cannot handle this data type: { typekey_shape } , { typestr } "
3296
3291
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 )
3299
3295
rawmode = mode
3296
+ else :
3297
+ mode = typemode
3300
3298
if mode in ["1" , "L" , "I" , "P" , "F" ]:
3301
3299
ndmax = 2
3302
3300
elif mode == "RGB" :
@@ -3393,29 +3391,29 @@ def fromqpixmap(im: ImageQt.QPixmap) -> ImageFile.ImageFile:
3393
3391
3394
3392
3395
3393
_fromarray_typemap = {
3396
- # (shape, typestr) => mode, rawmode
3394
+ # (shape, typestr) => mode, rawmode, color modes
3397
3395
# 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" ] ),
3416
3414
# 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" , [] ),
3419
3417
}
3420
3418
3421
3419
0 commit comments