Skip to content

Commit b322364

Browse files
committed
Only deprecate fromarray mode for changing data types
1 parent 77f3a09 commit b322364

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

Tests/test_image_array.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ def __init__(self, arr_params: dict[str, Any]) -> None:
101101
self.__array_interface__ = arr_params
102102

103103
with pytest.raises(ValueError):
104-
wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1)})
105-
with pytest.warns(DeprecationWarning, match="'mode' parameter"):
106-
Image.fromarray(wrapped, "L")
104+
wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1), "typestr": "|u1"})
105+
Image.fromarray(wrapped, "L")
107106

108107

109108
def test_fromarray_palette() -> None:
@@ -112,9 +111,16 @@ def test_fromarray_palette() -> None:
112111
a = numpy.array(i)
113112

114113
# Act
115-
with pytest.warns(DeprecationWarning, match="'mode' parameter"):
116-
out = Image.fromarray(a, "P")
114+
out = Image.fromarray(a, "P")
117115

118116
# Assert that the Python and C palettes match
119117
assert out.palette is not None
120118
assert len(out.palette.colors) == len(out.im.getpalette()) / 3
119+
120+
121+
def test_deprecation() -> None:
122+
a = numpy.array(im.convert("L"))
123+
with pytest.warns(
124+
DeprecationWarning, match="'mode' parameter for changing data types"
125+
):
126+
Image.fromarray(a, "1")

src/PIL/Image.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,19 +3275,9 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
32753275
transferred. This means that P and PA mode images will lose their palette.
32763276
32773277
: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.
32913281
32923282
See: :ref:`concept-modes` for general information about modes.
32933283
:returns: An image object.
@@ -3298,21 +3288,28 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
32983288
shape = arr["shape"]
32993289
ndim = len(shape)
33003290
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:
33053298
msg = "Cannot handle this data type"
33063299
raise TypeError(msg) from e
3300+
if typekey is not None:
33073301
try:
3308-
mode, rawmode = _fromarray_typemap[typekey]
3302+
typemode, rawmode, color_modes = _fromarray_typemap[typekey]
33093303
except KeyError as e:
33103304
typekey_shape, typestr = typekey
33113305
msg = f"Cannot handle this data type: {typekey_shape}, {typestr}"
33123306
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)
33153310
rawmode = mode
3311+
else:
3312+
mode = typemode
33163313
if mode in ["1", "L", "I", "P", "F"]:
33173314
ndmax = 2
33183315
elif mode == "RGB":
@@ -3409,29 +3406,29 @@ def fromqpixmap(im: ImageQt.QPixmap) -> ImageFile.ImageFile:
34093406

34103407

34113408
_fromarray_typemap = {
3412-
# (shape, typestr) => mode, rawmode
3409+
# (shape, typestr) => mode, rawmode, color modes
34133410
# 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"]),
34323429
# 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", []),
34353432
}
34363433

34373434

0 commit comments

Comments
 (0)