Skip to content

Commit 5b2a72a

Browse files
committed
Merge pull request #1224 from WeatherGod/imshow_alpha
Imsave alpha
2 parents d804231 + 22c4a19 commit 5b2a72a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/matplotlib/image.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
12461246
If *format* is *None* and *fname* is a string, the output
12471247
format is deduced from the extension of the filename.
12481248
*arr*:
1249-
A 2D array.
1249+
An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
12501250
Keyword arguments:
12511251
*vmin*/*vmax*: [ None | scalar ]
12521252
*vmin* and *vmax* set the color scaling for the image by fixing the
@@ -1269,7 +1269,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
12691269
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
12701270
from matplotlib.figure import Figure
12711271

1272-
figsize = [x / float(dpi) for x in arr.shape[::-1]]
1272+
figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
12731273
fig = Figure(figsize=figsize, dpi=dpi, frameon=False)
12741274
canvas = FigureCanvas(fig)
12751275
im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)

lib/matplotlib/tests/test_image.py

+20
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ def test_imsave():
128128

129129
assert_array_equal(arr_dpi1, arr_dpi100)
130130

131+
def test_imsave_color_alpha():
132+
# The goal is to test that imsave will accept arrays with ndim=3 where
133+
# the third dimension is color and alpha without raising any exceptions
134+
from numpy import random
135+
random.seed(1)
136+
data = random.rand(256, 128, 4)
137+
138+
buff = io.BytesIO()
139+
plt.imsave(buff, data)
140+
141+
buff.seek(0)
142+
arr_buf = plt.imread(buff)
143+
144+
assert arr_buf.shape == data.shape
145+
146+
# Unfortunately, the AGG process "flattens" the RGBA data
147+
# into an equivalent RGB data with no transparency. So we
148+
# Can't directly compare the arrays like we could in some
149+
# other imsave tests.
150+
131151
@image_comparison(baseline_images=['image_clip'])
132152
def test_image_clip():
133153
from math import pi

0 commit comments

Comments
 (0)