Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9a57241

Browse filesBrowse files
ksundenmeeseeksmachine
authored andcommitted
Backport PR #29931: Allow Python native sequences in Matplotlib imsave().
1 parent a595375 commit 9a57241
Copy full SHA for 9a57241

File tree

2 files changed

+27
-1
lines changed
Filter options

2 files changed

+27
-1
lines changed

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15361536
extension of *fname*, if any, and from :rc:`savefig.format` otherwise.
15371537
If *format* is set, it determines the output format.
15381538
arr : array-like
1539-
The image data. The shape can be one of
1539+
The image data. Accepts NumPy arrays or sequences
1540+
(e.g., lists or tuples). The shape can be one of
15401541
MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).
15411542
vmin, vmax : float, optional
15421543
*vmin* and *vmax* set the color scaling for the image by fixing the
@@ -1567,6 +1568,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15671568
default 'Software' key.
15681569
"""
15691570
from matplotlib.figure import Figure
1571+
1572+
# Normalizing input (e.g., list or tuples) to NumPy array if needed
1573+
arr = np.asanyarray(arr)
1574+
15701575
if isinstance(fname, os.PathLike):
15711576
fname = os.fspath(fname)
15721577
if format is None:

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,27 @@ def test_imsave(fmt):
184184
assert_array_equal(arr_dpi1, arr_dpi100)
185185

186186

187+
def test_imsave_python_sequences():
188+
# Tests saving an image with data passed using Python sequence types
189+
# such as lists or tuples.
190+
191+
# RGB image: 3 rows × 2 columns, with float values in [0.0, 1.0]
192+
img_data = [
193+
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
194+
[(0.0, 0.0, 1.0), (1.0, 1.0, 0.0)],
195+
[(0.0, 1.0, 1.0), (1.0, 0.0, 1.0)],
196+
]
197+
198+
buff = io.BytesIO()
199+
plt.imsave(buff, img_data, format="png")
200+
buff.seek(0)
201+
read_img = plt.imread(buff)
202+
203+
assert_array_equal(
204+
np.array(img_data),
205+
read_img[:, :, :3] # Drop alpha if present
206+
)
207+
187208
@pytest.mark.parametrize("origin", ["upper", "lower"])
188209
def test_imsave_rgba_origin(origin):
189210
# test that imsave always passes c-contiguous arrays down to pillow

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.