Skip to content

Backport PR #29931 on branch v3.10.x (Allow Python native sequences in Matplotlib imsave().) #29933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
extension of *fname*, if any, and from :rc:`savefig.format` otherwise.
If *format* is set, it determines the output format.
arr : array-like
The image data. The shape can be one of
The image data. Accepts NumPy arrays or sequences
(e.g., lists or tuples). The shape can be one of
MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).
vmin, vmax : float, optional
*vmin* and *vmax* set the color scaling for the image by fixing the
Expand Down Expand Up @@ -1567,6 +1568,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
default 'Software' key.
"""
from matplotlib.figure import Figure

# Normalizing input (e.g., list or tuples) to NumPy array if needed
arr = np.asanyarray(arr)

if isinstance(fname, os.PathLike):
fname = os.fspath(fname)
if format is None:
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ def test_imsave(fmt):
assert_array_equal(arr_dpi1, arr_dpi100)


def test_imsave_python_sequences():
# Tests saving an image with data passed using Python sequence types
# such as lists or tuples.

# RGB image: 3 rows × 2 columns, with float values in [0.0, 1.0]
img_data = [
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
[(0.0, 0.0, 1.0), (1.0, 1.0, 0.0)],
[(0.0, 1.0, 1.0), (1.0, 0.0, 1.0)],
]

buff = io.BytesIO()
plt.imsave(buff, img_data, format="png")
buff.seek(0)
read_img = plt.imread(buff)

assert_array_equal(
np.array(img_data),
read_img[:, :, :3] # Drop alpha if present
)


@pytest.mark.parametrize("origin", ["upper", "lower"])
def test_imsave_rgba_origin(origin):
# test that imsave always passes c-contiguous arrays down to pillow
Expand Down
Loading