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 @@ </tr> <tr> <th align="left"> + <a href="api/pyplot_api.html#matplotlib.pyplot.imsave">imsave</a> + + </th> + + <td align="left"> + save array as an image file + </td> + + </tr> + <tr> + <th align="left"> <a href="api/pyplot_api.html#matplotlib.pyplot.imshow">imshow</a> </th> 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 available depend on the backend being used. + Arguments: + *fname*: + A string containing a path to a filename, or a Python file-like object. + If *format* is *None* and *fname* is a string, the output + format is deduced from the extension of the filename. + *arr*: + A 2D array. + Keyword arguments: + *clims*: + clims is a tuple (vmin, vmax) that sets the color scaling for the image. + If either *vmin* or *vmax* is None, the image min/max respectively + will be used for color scaling. + *cmap*: + cmap is a colors.Colormap instance, eg cm.jet. + If None, default to the rc image.cmap value. + *format*: + One of the file extensions supported by the active + backend. Most backends support png, pdf, ps, eps and svg. + *origin* + [ 'upper' | 'lower' ] Indicates where the [0,0] index of + the array is in the upper left or lower left corner of + the axes. Defaults to the rc image.origin value. + """ + from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas + from matplotlib.figure import Figure + + fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False) + canvas = FigureCanvas(fig) + ax = fig.add_subplot(111) + ax.set_axis_off() + if clims is None: + vmin = vmax = None + else: + vmin,vmax = clims + fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin) + fig.savefig(fname, dpi=1, format=format) + + def pil_to_array( pilImage ): """ load a PIL image and return it as a numpy array of uint8. For
------------------------------------------------------------------------------ Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM) software. With Adobe AIR, Ajax developers can use existing skills and code to build responsive, highly engaging applications that combine the power of local resources and data with the reach of the web. Download the Adobe AIR SDK and Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel