tags:

views:

445

answers:

2

I currently have a PIL Image that I'd like to display on a PyQt window. I know this must be easy, but I can't find anywhere how to do it. Could anyone give me a hand on this? Here is the code of the window I currently have:

import sys
from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Window')


app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

Edit: According to Rapid Gui Programming with Qt and Python:

According to PyQt’s documentation, QPixmaps are optimized for on-screen display (so they are fast to draw), and QImages are optimized for editing (which is why we have used them to hold the image data).

I have a complex algorithm that will generate pictures I want to show on my window. They will be created pretty fast, so to the user they will look just like an animation (there can be like 15+, 20+ of them per second). Should I then use QPixmaps or QImages?

+1  A: 

Regarging to this discussion, the fastest way would be to use GLPainter in order to benefit of the Graphic Card performance.

Natim
+1  A: 

try something like this, you can use http://effbot.python-hosting.com/file/pil/PIL/ImageQt.py to convert any pil image to qimage

import sys
from PyQt4 import QtGui
from PIL import Image

def get_pil_image(w, h):
    clr = chr(0)+chr(255)+chr(0)
    im = Image.fromstring("RGB", (w,h), clr*(w*h))
    return im

def pil2qpixmap(pil_image):
    w, h = pil_image.size
    data = pil_image.tostring("raw", "BGRX")
    qimage = QtGui.QImage(data, w, h, QtGui.QImage.Format_RGB32)
    qpixmap = QtGui.QPixmap(w,h)
    pix = QtGui.QPixmap.fromImage(qimage)
    return pix

class ImageLabel(QtGui.QLabel):
    def __init__(self, parent=None):
        QtGui.QLabel.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Window')

        self.pix = pil2qpixmap(get_pil_image(50,50))
        self.setPixmap(self.pix)

app = QtGui.QApplication(sys.argv)
ImageLabel = ImageLabel()
ImageLabel.show()
sys.exit(app.exec_())
Anurag Uniyal