Memory issues with imshow

Dear all,

I am trying to write a script to be used with our microscope, stitching images of various magnifications together to yield a big picture of a sample. The preprocessing involves operations like rotating the picture etc., and finally those pictures are being plotted using imshow.

Unfortunately, I am running into memory problems, e.g.:

C:\Python26\lib\site-packages\PIL\ DeprecationWarning: integer argument expected, got float
  im =, resample)
Traceback (most recent call last):
  File "F:\Procs\", line 1093, in <module>
  File "F:\Procs\", line 1052, in process_images
  File "F:\Procs\", line 145, in stitch_images
  File "C:\Python26\lib\site-packages\matplotlib\", line 352, in draw
  File "C:\Python26\lib\site-packages\matplotlib\backends\", line 313, in draw
    self.renderer = self.get_renderer()
  File "C:\Python26\lib\site-packages\matplotlib\backends\", line 324, in get_renderer
    self.renderer = RendererAgg(w, h, self.figure.dpi)
  File "C:\Python26\lib\site-packages\matplotlib\backends\", line 59, in __init__
    self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
RuntimeError: Could not allocate memory for image


Traceback (most recent call last):
  File "F:\Procs\", line 1093, in <module>
  File "F:\Procs\", line 1052, in process_images
  File "F:\Procs\", line 142, in stitch_images
  File "C:\Python26\lib\site-packages\matplotlib\", line 2046, in imshow
    ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
  File "C:\Python26\lib\site-packages\matplotlib\", line 6275, in imshow
  File "C:\Python26\lib\site-packages\matplotlib\", line 291, in set_data
    self._A = pil_to_array(A)
  File "C:\Python26\lib\site-packages\matplotlib\", line 856, in pil_to_array
    x = toarray(im)
  File "C:\Python26\lib\site-packages\matplotlib\", line 831, in toarray
    x = np.fromstring(x_str,np.uint8)

I already implemented some downscaling of the original images (ca. 3200 x 2400 pixels), to roughly match the figures dpi-setting. But this does not seem to be the only issue. The script does work for dpi of 600 or 150 for 11 individual images, yielding e.g. a 23 MB file with 600 dpi and 36 Megapixels. But it fails for e.g. 35 images even for 75 dpi.

I was trying to throw away any unneccessary data using del + triggering the garbage collection, but this did not help beyond a certain point. Maybe somebody could tell me what kind of limitations there are using imshow to plot a lot of images together, and how to improve?

Some more info: I am using Windows. Just by judging from the task-manager, the preprocessing is not the problem. But *plotting* the images using imshow seems to cause an increase of memory consumption of the task of 32-33 MB *each* time. Somewhere around a total of 1.3 - 1.5 Gigs the process dies ...

Thanks in advance,



Dr. Gerd Wellenreuther
beamline scientist P06 "Hard X-Ray Micro/Nano-Probe"
Petra III project
Notkestr. 85
22603 Hamburg

Tel.: + 49 40 8998 5701