MemoryError when creating an ImageItem

108 views
Skip to first unread message

Gonzalo Bonigo

unread,
Mar 17, 2015, 9:17:00 AM3/17/15
to pyqt...@googlegroups.com, naivoarevalo
Hello.

We work on a remote sensing project, so we have to deal with very large images. We need to show an image of a size no bigger than 8000x8000x3 (8 bits per pixel), and we get a MemoryError when we attempt to create an ImageItem. Since the images are so big, we are constantly checking the amount of used memory, and we are sure that we have enough memory to do the work we need. So, this is not a run out memory problem, or at least this is what I think. The problem occurs only on Windows (we are using Windows 7 32 and 64 bits), but on Linux (Ubuntu 14.04) it works well. We are using python 3.3 and 3.4 by the way.

We isolated the problem into the following example code:
from pyqtgraph.Qt import QtCore, QtGui
from PIL import Image
import numpy as np
import pyqtgraph as pg

app = QtGui.QApplication([])

## Create window with GraphicsView widget
w = pg.GraphicsView()
w.show()
w.resize(800, 800)
w.setWindowTitle('pyqtgraph example: Draw')

view = pg.ViewBox()
w.setCentralItem(view)

## lock the aspect ratio
view.setAspectLocked(True)

image_rgb = Image.new(r'RGB', (8000, 8000))
image_rgb.save('image.tiff')
image_array = np.asarray(Image.open('image.tiff'))
image_item = pg.ImageItem(image_array)
view.addItem(image_item)

## Set initial view bounds
view.setRange(QtCore.QRectF(0, 0, 200, 200))

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

And this is the error we get:
C:\Users\...\Desktop>python example.py
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 309, in paint
    self.render()
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 301, in render
    argb, alpha = fn.makeARGB(image.transpose((1, 0, 2)[:image.ndim]), lut=lut, levels=self.levels)
  File "C:\Python33\lib\site-packages\pyqtgraph\functions.py", line 955, in makeARGB
    data = np.clip(data, 0, 255).astype(np.ubyte)
  File "C:\Python33\lib\site-packages\numpy\core\fromnumeric.py", line 1618, in clip
    return clip(a_min, a_max, out)
MemoryError
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\pyqtgraph\functions.py", line 790, in rescaleData
    raise Exception('Weave is disabled; falling back to slower version.')
Exception: Weave is disabled; falling back to slower version.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 309, in paint
    self.render()
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 301, in render
    argb, alpha = fn.makeARGB(image.transpose((1, 0, 2)[:image.ndim]), lut=lut, levels=self.levels)
  File "C:\Python33\lib\site-packages\pyqtgraph\functions.py", line 946, in makeARGB
    data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=int)
  File "C:\Python33\lib\site-packages\pyqtgraph\functions.py", line 833, in rescaleData
    d2 = d2.astype(dtype)
MemoryError

One strange thing is that if you reduce enough the image size it starts to work, but this apparent "maximum size" in which the example works change from one machine to another.

We run out of ides, so any help will be greatly appreciated.

Regards.
Gonzalo.
Reply all
Reply to author
Forward
0 new messages