I have a PyQT4 application that displays medium sized images in a Matplotlib shape. The test image I'm showing is about 5 MB (2809 x 1241 pixels). I read the data using GDAL. The image is read into an array with hidden nodata values. This is then displayed with normalized values ββand a given color palette.
It seems that an excessive amount of memory is being used to display a 5 megabyte file. I see that to display this image in full resolution requires 140 MB of memory. (an application with imshow commented on the used 60 MB of memory, against 206 with it). The problem worsens as the images are displayed on several figures, since each uses an additional 200 m of memory. About 3 or 4 images shown, applications begin to get bogged down when memory usage falls into the range of 700-900 mb.
I understand that matplotlib should store all the pixels, even if it only displays a reduced subset according to the screen resolution. I will probably end up writing routines to read only the number of pixels according to the size of the figure. But since this application will display up to 8 cards on 8 separate screens, I am worried that it still uses excessive memory.
So my questions are:
1) Does it look like an excessive amount of memory that will be used to display a simple image with a code image? This is with me.
2) Is there something I could do to reduce the use of this memory? For example, using integer data types, freeing memory, etc.
3) What other strategies should be used to work with this memory usage? For example, downsampling (may not be very effective at 1900x1200 full screen resolution), switching to 64-bit architecture, etc.
Thanks, Code below
import sys, os, random from PyQt4.QtCore import * from PyQt4.QtGui import * import matplotlib from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar from matplotlib.figure import Figure import matplotlib.colors as colors import numpy as np from osgeo import gdal, gdalconst gridfile = r"i:\vistrails\workingfiles\secondseason\secondseason_workfile_2012_02_28b\brt_1\brt_prob_map.tif" class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.create_main_frame() ds = gdal.Open(gridfile, gdal.GA_ReadOnly) ary = ds.GetRasterBand(1).ReadAsArray(buf_ysize=500, buf_xsize=300) ndval = ds.GetRasterBand(1).GetNoDataValue() rasterdata = np.ma.masked_array(ary, mask=(ary==ndval)) del ary self.axes.imshow(rasterdataint, cmap=matplotlib.cm.jet) del rasterdata def create_main_frame(self): self.main_frame = QWidget()