Hi,
I've attached a patch against the mpl head in response to John's
suggestion and helpful pointers on the users list. This adds a new
function imsave to complement imread in the image module. I've also
exposed it in the pyplot interface.
If this is presumptuous, feel free to remove it from the patch or ask me
to remove it. This is my first contribution to matplotlib for a while
and my first attempt at a patch like this.
Because I don't build matplotlib from source, I've done what testing I
can without fully rebuilding matplotlib. I'd be reluctant to have this
patch applied without someone else checking it first. It's pretty simple
so applying it locally and running the demo file should be a good enough
test.
thanks,
Gary Ruben
Index: matplotlib/CHANGELOG
===
--- matplotlib/CHANGELOG(revision 6889)
+++ matplotlib/CHANGELOG(working copy)
@@ -1,3 +1,6 @@
+2009-02-08 Added a new imsave function to image.py and exposed it in
+ the pyplot interface - GR
+
2009-02-04 Some reorgnization of the legend code. anchored_text.py
added as an example. - JJL
Index: matplotlib/doc/_templates/index.html
===
--- matplotlib/doc/_templates/index.html(revision 6889)
+++ matplotlib/doc/_templates/index.html(working copy)
@@ -567,6 +567,17 @@
+imsave
+
+
+
+
+save array as an image file
+
+
+
+
+
imshow
Index: matplotlib/doc/api/api_changes.rst
===
--- matplotlib/doc/api/api_changes.rst (revision 6889)
+++ matplotlib/doc/api/api_changes.rst (working copy)
@@ -19,6 +19,9 @@
Changes for 0.98.x
==
+* Added new :func:`matplotlib.image.imsave` and exposed it to the
+ :mod:`matplotlib.pyplot` interface.
+
* Remove support for pyExcelerator in exceltools -- use xlwt
instead
Index: matplotlib/lib/matplotlib/pyplot.py
===
--- matplotlib/lib/matplotlib/pyplot.py (revision 6889)
+++ matplotlib/lib/matplotlib/pyplot.py (working copy)
@@ -6,6 +6,7 @@
from matplotlib.figure import Figure, figaspect
from matplotlib.backend_bases import FigureCanvasBase
from matplotlib.image import imread as _imread
+from matplotlib.image import imsave as _imsave
from matplotlib import rcParams, rcParamsDefault, get_backend
from matplotlib.rcsetup import interactive_bk as _interactive_bk
from matplotlib.artist import getp, get, Artist
@@ -1181,6 +1182,7 @@
legend add a legend to the axes
loglog a log log plot
imread load image file into array
+imsave save array as an image file
imshow plot image data
matshow display a matrix in a new figure preserving aspect
pcolor make a pseudocolor plot
@@ -1230,7 +1232,7 @@
def get_plot_commands(): return ( 'axes', 'axis', 'bar', 'boxplot', 'cla',
'clf',
'close', 'colorbar', 'cohere', 'csd', 'draw', 'errorbar',
'figlegend', 'figtext', 'figimage', 'figure', 'fill', 'gca',
-'gcf', 'gci', 'get', 'gray', 'barh', 'jet', 'hist', 'hold', 'imread',
+'gcf', 'gci', 'get', 'gray', 'barh', 'jet', 'hist', 'hold', 'imread',
'imsave',
'imshow', 'legend', 'loglog', 'quiver', 'rc', 'pcolor', 'pcolormesh',
'plot', 'psd',
'savefig', 'scatter', 'set', 'semilogx', 'semilogy', 'show',
'specgram', 'stem', 'subplot', 'table', 'text', 'title', 'xlabel',
@@ -1370,6 +1372,11 @@
if _imread.__doc__ is not None:
imread.__doc__ = dedent(_imread.__doc__)
+def imsave(*args, **kwargs):
+return _imsave(*args, **kwargs)
+if _imsave.__doc__ is not None:
+imsave.__doc__ = dedent(_imsave.__doc__)
+
def matshow(A, fignum=None, **kw):
"""
Display an array as a matrix in a new figure window.
Index: matplotlib/lib/matplotlib/pylab.py
===
--- matplotlib/lib/matplotlib/pylab.py (revision 6889)
+++ matplotlib/lib/matplotlib/pylab.py (working copy)
@@ -50,6 +50,7 @@
ion - turn interaction mode on
isinteractive - return True if interaction mode is on
imread - load image file into array
+ imsave - save array as an image file
imshow - plot image data
ishold - return the hold state of the current axes
legend - make an axes legend
Index: matplotlib/lib/matplotlib/image.py
===
--- matplotlib/lib/matplotlib/image.py (revision 6889)
+++ matplotlib/lib/matplotlib/image.py (working copy)
@@ -747,7 +747,49 @@
return handler(fname)
+def imsave(fname, arr, clims=None, cmap=None, format=None, origin=None):
+"""
+Saves a 2D :class:`numpy.array` as an image with one pixel per element.
+The output formats