From e6af31e709eb61c553d13fb8648772379211f250 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 14 Jun 2025 16:09:11 +1000 Subject: [PATCH] Deprecate fromarray mode argument --- Tests/test_image_array.py | 6 ++++-- docs/deprecations.rst | 8 ++++++++ docs/releasenotes/11.3.0.rst | 7 ++++--- src/PIL/Image.py | 3 ++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index eb2309e0f..2c71dceb8 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -101,7 +101,8 @@ def test_fromarray_strides_without_tobytes() -> None: with pytest.raises(ValueError): wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1)}) - Image.fromarray(wrapped, "L") + with pytest.warns(DeprecationWarning): + Image.fromarray(wrapped, "L") def test_fromarray_palette() -> None: @@ -110,7 +111,8 @@ def test_fromarray_palette() -> None: a = numpy.array(i) # Act - out = Image.fromarray(a, "P") + with pytest.warns(DeprecationWarning): + out = Image.fromarray(a, "P") # Assert that the Python and C palettes match assert out.palette is not None diff --git a/docs/deprecations.rst b/docs/deprecations.rst index 0490ba439..a5d89408b 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -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 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 ---------------- diff --git a/docs/releasenotes/11.3.0.rst b/docs/releasenotes/11.3.0.rst index b0595def9..f0fa8c858 100644 --- a/docs/releasenotes/11.3.0.rst +++ b/docs/releasenotes/11.3.0.rst @@ -23,10 +23,11 @@ TODO 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 =========== diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 7e9540e48..0be9e8ce4 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3272,7 +3272,7 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image: :param obj: Object with array interface :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 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}" raise TypeError(msg) from e else: + deprecate("'mode' parameter", 13) rawmode = mode if mode in ["1", "L", "I", "P", "F"]: ndmax = 2