Displayin an Image in a QGraphicsScene

后端 未结 1 529
灰色年华
灰色年华 2020-12-30 15:59

I have a short script that modifies an image with PIL several times. I would like to be able to display the intermediate steps as it finishes with them, so I added a QGraph

相关标签:
1条回答
  • 2020-12-30 16:39

    It is hard to determine your problem without loop code and working example. But i have similar test application, hope it will help.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import Image
    import ImageQt
    import ImageEnhance
    import time
    
    class TestWidget(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self.scene = QGraphicsScene()
            self.view = QGraphicsView(self.scene)
            self.button = QPushButton("Do test")
    
            layout = QVBoxLayout()
            layout.addWidget(self.button)
            layout.addWidget(self.view)
            self.setLayout(layout)
    
            self.button.clicked.connect(self.do_test)
    
        def do_test(self):
            img = Image.open('image.png')
            enhancer = ImageEnhance.Brightness(img)
            for i in range(1, 8):
                img = enhancer.enhance(i)
                self.display_image(img)
                QCoreApplication.processEvents()  # let Qt do his work
                time.sleep(0.5)
    
        def display_image(self, img):
            self.scene.clear()
            w, h = img.size
            self.imgQ = ImageQt.ImageQt(img)  # we need to hold reference to imgQ, or it will crash
            pixMap = QPixmap.fromImage(self.imgQ)
            self.scene.addPixmap(pixMap)
            self.view.fitInView(QRectF(0, 0, w, h), Qt.KeepAspectRatio)
            self.scene.update()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        widget = TestWidget()
        widget.resize(640, 480)
        widget.show()
    
        sys.exit(app.exec_())
    

    The main points:

    • If you are doing some processing or sleep in loop, you need to call QCoreApplication.processEvents() to allow Qt to do updates.

    • I'm saving reference to ImageQt.ImageQt (self.imgQ) or it will crash.

    • As I understood, you are creating QGraphicsScene in each iteration, better solution to create it once and then call scene.clear().

    • Scaling pixmap just to display it sized and centered is expensive, QGraphicsView.fitInView() maked for this purpose.

    0 讨论(0)
提交回复
热议问题