Skip to content

Imsave alpha #1224

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
merged 2 commits into from
Sep 13, 2012
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
4 changes: 2 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
If *format* is *None* and *fname* is a string, the output
format is deduced from the extension of the filename.
*arr*:
A 2D array.
An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
Keyword arguments:
*vmin*/*vmax*: [ None | scalar ]
*vmin* and *vmax* set the color scaling for the image by fixing the
Expand All @@ -1266,7 +1266,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

figsize = [x / float(dpi) for x in arr.shape[::-1]]
figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
fig = Figure(figsize=figsize, dpi=dpi, frameon=False)
canvas = FigureCanvas(fig)
im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
Expand Down
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ def test_imsave():

assert_array_equal(arr_dpi1, arr_dpi100)

def test_imsave_color_alpha():
# The goal is to test that imsave will accept arrays with ndim=3 where
# the third dimension is color and alpha without raising any exceptions
from numpy import random
random.seed(1)
data = random.rand(256, 128, 4)

buff = io.BytesIO()
plt.imsave(buff, data)

buff.seek(0)
arr_buf = plt.imread(buff)

assert arr_buf.shape == data.shape

# Unfortunately, the AGG process "flattens" the RGBA data
# into an equivalent RGB data with no transparency. So we
# Can't directly compare the arrays like we could in some
# other imsave tests.

@image_comparison(baseline_images=['image_clip'])
def test_image_clip():
from math import pi
Expand Down