@@ -3275,19 +3275,9 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
3275
3275
transferred. This means that P and PA mode images will lose their palette.
3276
3276
3277
3277
:param obj: Object with array interface
3278
- :param mode: Optional mode to use when reading ``obj``. Will be determined from
3279
- type if ``None``. Deprecated.
3280
-
3281
- This will not be used to convert the data after reading, but will be used to
3282
- change how the data is read::
3283
-
3284
- from PIL import Image
3285
- import numpy as np
3286
- a = np.full((1, 1), 300)
3287
- im = Image.fromarray(a, mode="L")
3288
- im.getpixel((0, 0)) # 44
3289
- im = Image.fromarray(a, mode="RGB")
3290
- im.getpixel((0, 0)) # (44, 1, 0)
3278
+ :param mode: Optional mode to use when reading ``obj``. Since pixel values do not
3279
+ contain information about palettes or color spaces, this can be used to place
3280
+ grayscale L mode data within a P mode image, or read RGB data as YCbCr.
3291
3281
3292
3282
See: :ref:`concept-modes` for general information about modes.
3293
3283
:returns: An image object.
@@ -3298,21 +3288,28 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
3298
3288
shape = arr ["shape" ]
3299
3289
ndim = len (shape )
3300
3290
strides = arr .get ("strides" , None )
3301
- if mode is None :
3302
- try :
3303
- typekey = (1 , 1 ) + shape [2 :], arr ["typestr" ]
3304
- except KeyError as e :
3291
+ try :
3292
+ typekey = (1 , 1 ) + shape [2 :], arr ["typestr" ]
3293
+ except KeyError as e :
3294
+ if mode is not None :
3295
+ typekey = None
3296
+ color_modes = []
3297
+ else :
3305
3298
msg = "Cannot handle this data type"
3306
3299
raise TypeError (msg ) from e
3300
+ if typekey is not None :
3307
3301
try :
3308
- mode , rawmode = _fromarray_typemap [typekey ]
3302
+ typemode , rawmode , color_modes = _fromarray_typemap [typekey ]
3309
3303
except KeyError as e :
3310
3304
typekey_shape , typestr = typekey
3311
3305
msg = f"Cannot handle this data type: { typekey_shape } , { typestr } "
3312
3306
raise TypeError (msg ) from e
3313
- else :
3314
- deprecate ("'mode' parameter" , 13 )
3307
+ if mode is not None :
3308
+ if mode != typemode and mode not in color_modes :
3309
+ deprecate ("'mode' parameter for changing data types" , 13 )
3315
3310
rawmode = mode
3311
+ else :
3312
+ mode = typemode
3316
3313
if mode in ["1" , "L" , "I" , "P" , "F" ]:
3317
3314
ndmax = 2
3318
3315
elif mode == "RGB" :
@@ -3409,29 +3406,29 @@ def fromqpixmap(im: ImageQt.QPixmap) -> ImageFile.ImageFile:
3409
3406
3410
3407
3411
3408
_fromarray_typemap = {
3412
- # (shape, typestr) => mode, rawmode
3409
+ # (shape, typestr) => mode, rawmode, color modes
3413
3410
# first two members of shape are set to one
3414
- ((1 , 1 ), "|b1" ): ("1" , "1;8" ),
3415
- ((1 , 1 ), "|u1" ): ("L" , "L" ),
3416
- ((1 , 1 ), "|i1" ): ("I" , "I;8" ),
3417
- ((1 , 1 ), "<u2" ): ("I" , "I;16" ),
3418
- ((1 , 1 ), ">u2" ): ("I" , "I;16B" ),
3419
- ((1 , 1 ), "<i2" ): ("I" , "I;16S" ),
3420
- ((1 , 1 ), ">i2" ): ("I" , "I;16BS" ),
3421
- ((1 , 1 ), "<u4" ): ("I" , "I;32" ),
3422
- ((1 , 1 ), ">u4" ): ("I" , "I;32B" ),
3423
- ((1 , 1 ), "<i4" ): ("I" , "I;32S" ),
3424
- ((1 , 1 ), ">i4" ): ("I" , "I;32BS" ),
3425
- ((1 , 1 ), "<f4" ): ("F" , "F;32F" ),
3426
- ((1 , 1 ), ">f4" ): ("F" , "F;32BF" ),
3427
- ((1 , 1 ), "<f8" ): ("F" , "F;64F" ),
3428
- ((1 , 1 ), ">f8" ): ("F" , "F;64BF" ),
3429
- ((1 , 1 , 2 ), "|u1" ): ("LA" , "LA" ),
3430
- ((1 , 1 , 3 ), "|u1" ): ("RGB" , "RGB" ),
3431
- ((1 , 1 , 4 ), "|u1" ): ("RGBA" , "RGBA" ),
3411
+ ((1 , 1 ), "|b1" ): ("1" , "1;8" , [] ),
3412
+ ((1 , 1 ), "|u1" ): ("L" , "L" , [ "P" ] ),
3413
+ ((1 , 1 ), "|i1" ): ("I" , "I;8" , [] ),
3414
+ ((1 , 1 ), "<u2" ): ("I" , "I;16" , [] ),
3415
+ ((1 , 1 ), ">u2" ): ("I" , "I;16B" , [] ),
3416
+ ((1 , 1 ), "<i2" ): ("I" , "I;16S" , [] ),
3417
+ ((1 , 1 ), ">i2" ): ("I" , "I;16BS" , [] ),
3418
+ ((1 , 1 ), "<u4" ): ("I" , "I;32" , [] ),
3419
+ ((1 , 1 ), ">u4" ): ("I" , "I;32B" , [] ),
3420
+ ((1 , 1 ), "<i4" ): ("I" , "I;32S" , [] ),
3421
+ ((1 , 1 ), ">i4" ): ("I" , "I;32BS" , [] ),
3422
+ ((1 , 1 ), "<f4" ): ("F" , "F;32F" , [] ),
3423
+ ((1 , 1 ), ">f4" ): ("F" , "F;32BF" , [] ),
3424
+ ((1 , 1 ), "<f8" ): ("F" , "F;64F" , [] ),
3425
+ ((1 , 1 ), ">f8" ): ("F" , "F;64BF" , [] ),
3426
+ ((1 , 1 , 2 ), "|u1" ): ("LA" , "LA" , [ "La" , "PA" ] ),
3427
+ ((1 , 1 , 3 ), "|u1" ): ("RGB" , "RGB" , [ "YCbCr" , "LAB" , "HSV" ] ),
3428
+ ((1 , 1 , 4 ), "|u1" ): ("RGBA" , "RGBA" , [ "RGBa" ] ),
3432
3429
# shortcuts:
3433
- ((1 , 1 ), f"{ _ENDIAN } i4" ): ("I" , "I" ),
3434
- ((1 , 1 ), f"{ _ENDIAN } f4" ): ("F" , "F" ),
3430
+ ((1 , 1 ), f"{ _ENDIAN } i4" ): ("I" , "I" , [] ),
3431
+ ((1 , 1 ), f"{ _ENDIAN } f4" ): ("F" , "F" , [] ),
3435
3432
}
3436
3433
3437
3434
0 commit comments