我现在有一个PIL图片,我想显示一个PyQt的窗口上。我知道这一定是容易的,但我无法找到任何地方如何做到这一点。谁能给我一个手呢? 这里是窗口我现在有的代码:

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_())

编辑:根据快速GUI编程使用Qt和Python:

  

据的PyQt的文档,   QPixmaps是为屏幕上的优化   显示器(所以他们很快画出),   和QImages被编辑优化   (这就是为什么我们让他们习惯   保持图像数据)。

我有一个复杂的算法,将生成的照片我想表明我的窗户。他们将创造相当快,所以给用户他们看起来就像动画(也可以像15+,每秒他们20+)。应予然后使用QPixmaps或QImages?

有帮助吗?

解决方案

尝试这样的事情,你可以使用的http:// SVN .effbot.org /公共/东西/沙箱/ PIL / ImageQt.py 以任何PIL图像转换为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_())

其他提示

Regarging到此讨论,最快方式是使用GLPainter为了利于图形卡性能的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top