Using excess memory in Matplotlib imshow

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() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.dpi = 100 self.fig = Figure((5.0, 4.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.axes = self.fig.add_subplot(111) self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def main(): app = QApplication(sys.argv) form = AppForm() form.show() app.exec_() if __name__ == "__main__": main() 
+4
source share
1 answer

A memory issue using imshow() was seen as here .

1 / Update

As mentioned here, updating to the latest version of mpl may fix the problem .

2 / PIL

Alternatively, you can make from the PIL library.

When it goes into jpg files, imshow () uses PIL if installed. You can directly use the PIL module as described here .

+1
source

Source: https://habr.com/ru/post/1399280/


All Articles