runtime error; producing multiple images

Hi folks,

I have a question I couldn't solve so far. I'm trying to generate say 500 images
(png) without displaying them using imshow. I wrote a simple test function
(see below) which I call from a loop in which I create the data for the image in
a sequence. I systematically bump into a runtime error after saving about 188
files. After reading the (excellent) tutorial from Perry Greenfield and
Robert Jedrzejewski on interactive data analysis, I understand the need to clean
up by freeing memory but I must be missing something else.

Cheers,

Aurélien

def generateImshow(yarray,
                   xscansize,yscansize,
                   xscanstep,yscanstep,
                   outputfilename,
                   initimagesize = 10):
    '''Use this function to generate matplotlib images without displaying
them'''

    #reshape according to scan
    yarray = na.reshape(yarray,(yscansize,-1))

    #build image via matplotlib
    ximagesize = xscansize*xscanstep
    yimagesize = yscansize*yscanstep
    xyimageratio = float(ximagesize)/yimagesize
    print xyimageratio
    if xyimageratio > 1: ximagesize,yimagesize =
initimagesize,initimagesize*xyimageratio
    else: ximagesize,yimagesize = initimagesize*xyimageratio,initimagesize

    fig1 = pylab.figure(figsize=(ximagesize,yimagesize),dpi=100)
    #pylab.title('blahblah')
    im1 = pylab.imshow(yarray,
                 origin='lower',
                 #aspect='preserve',
                 #interpolation='nearest', #i.e. pixel
                 interpolation='bicubic', #i.e. smooth
                 #cmap=mpl.cm.jet, #not correct
                 #vmin=minvalue,
                 #vmax=maxvalue
                 )
    pylab.colorbar()
    pylab.bone()
    pylab.axis('off')

    #save figure
    #outputfilename = filename[:-4]+'.png'
    pylab.savefig(outputfilename)
    #pylab.cla()
    del im1
    pylab.close('all')