[PyQt] Converting numpy arrays to QPixmap

Alexander Borghgraef alexander.borghgraef.rma at gmail.com
Mon Jul 5 14:06:04 BST 2010


Hi all,

 Some time ago, I wrote a little image processing tool which worked on
numpy arrays. To display them in the gui, I used a procedure similar
to the following demo code:
from PyQt4.QtGui import *
import scipy, ImageQt, sys

app = QApplication(sys.argv)
l = scipy.lena()
p = QLabel()
q = QPixmap.fromImage(ImageQt.ImageQt(scipy.misc.toimage(l)))
p.setPixmap(q)
p.show()
app.exec_()

following the conversion chain:

ndarray l --> PIL image --> ImageQt object -->QPixmap q

This used to work on my kubuntu 9.10 machine, but now that I run it on
my new machine running fedora 13 with kde 4.4.4, it won't display the
images any more. The window appears, the QLabel that should contain
the image has the right size, but where the image should be it stays
white. Any ideas on what's causing this?

When I couldn't find a cause for this behaviour, I started looking for
alternatives. One solution turned out to be to feed the ndarray  data
into a QImage constructor, and bypass all of the conversion functions
gathered from different libraries. Much more elegant, but that made me
run into a different problem: my images are 8-bit grayscale images,
but the QImage constructor has no such format available. It has 32 bit
RBG(A) images, 8-bit indexed color images, and 1-bit mono, but no
8-bit grayscale. I could of course put 3 identical color images in an
RGB image, but that seems a bit inelegant. Any good solutions for
this?

-- 
Alex Borghgraef


More information about the PyQt mailing list