Skip to content

Commit df29a4e

Browse files
committed
FIX: ensure images are C order before passing to pillow
closes #28020
1 parent 2723052 commit df29a4e

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/matplotlib/image.py

+1
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
16401640
# we modify this below, so make a copy (don't modify caller's dict)
16411641
pil_kwargs = pil_kwargs.copy()
16421642
pil_shape = (rgba.shape[1], rgba.shape[0])
1643+
rgba = np.require(rgba, requirements='C')
16431644
image = PIL.Image.frombuffer(
16441645
"RGBA", pil_shape, rgba, "raw", "RGBA", 0, 1)
16451646
if format == "png":

lib/matplotlib/tests/test_image.py

+8
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ def test_imsave(fmt):
205205
assert_array_equal(arr_dpi1, arr_dpi100)
206206

207207

208+
@pytest.mark.parametrize("origin", ["upper", "lower"])
209+
def test_imsave_rgba_origin(origin):
210+
# test that imsave always passes c-contiguous arrays down to pillow
211+
buf = io.BytesIO()
212+
result = np.zeros((10, 10, 4), dtype='uint8')
213+
mimage.imsave(buf, arr=result, format="png", origin=origin)
214+
215+
208216
@pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"])
209217
def test_imsave_fspath(fmt):
210218
plt.imsave(Path(os.devnull), np.array([[0, 1]]), format=fmt)

0 commit comments

Comments
 (0)