Deprecate fromarray mode argument

This commit is contained in:
Andrew Murray 2025-06-14 16:09:11 +10:00
parent a3d91cb0ce
commit e6af31e709
4 changed files with 18 additions and 6 deletions

View File

@ -101,6 +101,7 @@ def test_fromarray_strides_without_tobytes() -> None:
with pytest.raises(ValueError): with pytest.raises(ValueError):
wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1)}) wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1)})
with pytest.warns(DeprecationWarning):
Image.fromarray(wrapped, "L") Image.fromarray(wrapped, "L")
@ -110,6 +111,7 @@ def test_fromarray_palette() -> None:
a = numpy.array(i) a = numpy.array(i)
# Act # Act
with pytest.warns(DeprecationWarning):
out = Image.fromarray(a, "P") out = Image.fromarray(a, "P")
# Assert that the Python and C palettes match # Assert that the Python and C palettes match

View File

@ -193,6 +193,14 @@ Image.Image.get_child_images()
method uses an image's file pointer, and so child images could only be retrieved from method uses an image's file pointer, and so child images could only be retrieved from
an :py:class:`PIL.ImageFile.ImageFile` instance. an :py:class:`PIL.ImageFile.ImageFile` instance.
Image.fromarray mode parameter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. deprecated:: 11.3.0
The ``mode`` parameter in :py:meth:`~PIL.Image.fromarray()` has been deprecated. The
mode can be automatically determined from the object's shape and type instead.
Removed features Removed features
---------------- ----------------

View File

@ -23,10 +23,11 @@ TODO
Deprecations Deprecations
============ ============
TODO Image.fromarray mode parameter
^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TODO The ``mode`` parameter in :py:meth:`~PIL.Image.fromarray()` has been deprecated. The
mode can be automatically determined from the object's shape and type instead.
API changes API changes
=========== ===========

View File

@ -3272,7 +3272,7 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
:param obj: Object with array interface :param obj: Object with array interface
:param mode: Optional mode to use when reading ``obj``. Will be determined from :param mode: Optional mode to use when reading ``obj``. Will be determined from
type if ``None``. type if ``None``. Deprecated.
This will not be used to convert the data after reading, but will be used to This will not be used to convert the data after reading, but will be used to
change how the data is read:: change how the data is read::
@ -3307,6 +3307,7 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
msg = f"Cannot handle this data type: {typekey_shape}, {typestr}" msg = f"Cannot handle this data type: {typekey_shape}, {typestr}"
raise TypeError(msg) from e raise TypeError(msg) from e
else: else:
deprecate("'mode' parameter", 13)
rawmode = mode rawmode = mode
if mode in ["1", "L", "I", "P", "F"]: if mode in ["1", "L", "I", "P", "F"]:
ndmax = 2 ndmax = 2