Hi all,

I've attached a candidate imsave() to complement imread() in the image.py module. Would my use of pyplot instead of the oo interface preclude its inclusion in image.py? Also, I noticed some problems when I ran the tests with the Wx backends with mpl 0.98.5.2 in Win32. Both of the Wx backends produce incorrect, but different, results.

Gary R.
import numpy as np
import matplotlib as mpl
# Backend tests - uncomment in turn
# mpl.use('Agg')
# mpl.use('TkAgg')
# mpl.use('WxAgg') # problem in Win32 with mpl 0.98.5.2
# mpl.use('Wx')    # several problems in Win32 with mpl 0.98.5.2
# mpl.use('GTK')
# mpl.use('GTKAgg')
import matplotlib.pyplot as plt
from matplotlib import rcParams
import matplotlib.image as mpi
from matplotlib import cm


def imsave(fname, arr, clims=None, cmap=None, format=None, origin=None):
    """
    Saves a 2D array as a bitmapped 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 sets the color scaling for the image.
        It is a tuple (vmin, vmax) that is passed through to the pyplot clim 
function.
        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.
      *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.
    """
    ydim, xdim = arr.shape
    if cmap is None: cmap = eval('cm.' + rcParams['image.cmap'])
    if origin is None: origin = rcParams['image.origin']
    f = plt.figure(figsize=(xdim,ydim), dpi=1)
    plt.axes([0,0,xdim,ydim])
    plt.axis('off')
    plt.figimage(arr, cmap=cmap, origin=origin)
    if clims is not None: plt.clim(*clims)
    plt.savefig(fname, dpi=1, format=format)


# tests
imsave('test1.png', np.tri(100), origin='lower')
imsave('test2.png', np.tri(100), origin='upper')
imsave('test3png', np.random.random((100,100)), cmap=cm.Oranges, format='png')
imsave('test4.png', 
np.hstack((np.tri(100)+np.tri(100)[:,::-1],np.vstack((np.eye(50),np.ones((50,50))*0.75)))),cmap=cm.gray)
imsave('test5.png', 
np.vstack((np.tri(100),np.hstack((np.eye(50),np.ones((50,50))*0.25)))),cmap=cm.gray)
imsave('test6.png', np.vstack((np.ones((100,100)),np.zeros((50,100)))))
imsave('test7.png', np.eye(2))
imsave('test8.png', np.array([[1]]))
------------------------------------------------------------------------------
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-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to