Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55e0c6b

Browse files
committed
Add imsave() to matplotlib.image, pyplot, and pylab. Patch from Gary Ruben.
svn path=/trunk/matplotlib/; revision=6899
1 parent 5faff4c commit 55e0c6b

6 files changed

Lines changed: 62 additions & 1 deletion

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2009-02-08 Added a new imsave function to image.py and exposed it in
2+
the pyplot interface - GR
3+
14
2009-02-04 Some reorgnization of the legend code. anchored_text.py
25
added as an example. - JJL
36

doc/_templates/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,17 @@ <h3>Plotting commands</h3> <br/>
564564
load image file into array
565565
</td>
566566

567+
</tr>
568+
<tr>
569+
<th align="left">
570+
<a href="api/pyplot_api.html#matplotlib.pyplot.imsave">imsave</a>
571+
572+
</th>
573+
574+
<td align="left">
575+
save array as an image file
576+
</td>
577+
567578
</tr>
568579
<tr>
569580
<th align="left">

doc/api/api_changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ list may help describe what changes may be necessary in your code.
1919

2020
Changes for 0.98.x
2121
==================
22+
* Added new :func:`matplotlib.image.imsave` and exposed it to the
23+
:mod:`matplotlib.pyplot` interface.
24+
2225
* Remove support for pyExcelerator in exceltools -- use xlwt
2326
instead
2427

lib/matplotlib/image.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,42 @@ def pilread():
747747
return handler(fname)
748748

749749

750+
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, origin=None):
751+
"""
752+
Saves a 2D :class:`numpy.array` as an image with one pixel per element.
753+
The output formats available depend on the backend being used.
754+
755+
Arguments:
756+
*fname*:
757+
A string containing a path to a filename, or a Python file-like object.
758+
If *format* is *None* and *fname* is a string, the output
759+
format is deduced from the extension of the filename.
760+
*arr*:
761+
A 2D array.
762+
Keyword arguments:
763+
*vmin*/*vmax*: [ None | scalar ]
764+
*vmin* and *vmax* set the color scaling for the image by fixing the
765+
values that map to the colormap color limits. If either *vmin* or *vmax*
766+
is None, that limit is determined from the *arr* min/max value.
767+
*cmap*:
768+
cmap is a colors.Colormap instance, eg cm.jet.
769+
If None, default to the rc image.cmap value.
770+
*format*:
771+
One of the file extensions supported by the active
772+
backend. Most backends support png, pdf, ps, eps and svg.
773+
*origin*
774+
[ 'upper' | 'lower' ] Indicates where the [0,0] index of
775+
the array is in the upper left or lower left corner of
776+
the axes. Defaults to the rc image.origin value.
777+
"""
778+
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
779+
from matplotlib.figure import Figure
780+
781+
fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
782+
canvas = FigureCanvas(fig)
783+
fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
784+
fig.savefig(fname, dpi=1, format=format)
785+
750786

751787
def pil_to_array( pilImage ):
752788
"""

lib/matplotlib/pylab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
ion - turn interaction mode on
5151
isinteractive - return True if interaction mode is on
5252
imread - load image file into array
53+
imsave - save array as an image file
5354
imshow - plot image data
5455
ishold - return the hold state of the current axes
5556
legend - make an axes legend

lib/matplotlib/pyplot.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from matplotlib.figure import Figure, figaspect
77
from matplotlib.backend_bases import FigureCanvasBase
88
from matplotlib.image import imread as _imread
9+
from matplotlib.image import imsave as _imsave
910
from matplotlib import rcParams, rcParamsDefault, get_backend
1011
from matplotlib.rcsetup import interactive_bk as _interactive_bk
1112
from matplotlib.artist import getp, get, Artist
@@ -1181,6 +1182,7 @@ def plotting():
11811182
legend add a legend to the axes
11821183
loglog a log log plot
11831184
imread load image file into array
1185+
imsave save array as an image file
11841186
imshow plot image data
11851187
matshow display a matrix in a new figure preserving aspect
11861188
pcolor make a pseudocolor plot
@@ -1230,7 +1232,7 @@ def plotting():
12301232
def get_plot_commands(): return ( 'axes', 'axis', 'bar', 'boxplot', 'cla', 'clf',
12311233
'close', 'colorbar', 'cohere', 'csd', 'draw', 'errorbar',
12321234
'figlegend', 'figtext', 'figimage', 'figure', 'fill', 'gca',
1233-
'gcf', 'gci', 'get', 'gray', 'barh', 'jet', 'hist', 'hold', 'imread',
1235+
'gcf', 'gci', 'get', 'gray', 'barh', 'jet', 'hist', 'hold', 'imread', 'imsave',
12341236
'imshow', 'legend', 'loglog', 'quiver', 'rc', 'pcolor', 'pcolormesh', 'plot', 'psd',
12351237
'savefig', 'scatter', 'set', 'semilogx', 'semilogy', 'show',
12361238
'specgram', 'stem', 'subplot', 'table', 'text', 'title', 'xlabel',
@@ -1370,6 +1372,11 @@ def imread(*args, **kwargs):
13701372
if _imread.__doc__ is not None:
13711373
imread.__doc__ = dedent(_imread.__doc__)
13721374

1375+
def imsave(*args, **kwargs):
1376+
return _imsave(*args, **kwargs)
1377+
if _imsave.__doc__ is not None:
1378+
imsave.__doc__ = dedent(_imsave.__doc__)
1379+
13731380
def matshow(A, fignum=None, **kw):
13741381
"""
13751382
Display an array as a matrix in a new figure window.

0 commit comments

Comments
 (0)