在 QGraphicsScene 中显示图像 [英] Displayin an Image in a QGraphicsScene

查看:423
本文介绍了在 QGraphicsScene 中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的脚本,可以多次使用 PIL 修改图像.我希望能够在完成它们时显示中间步骤,所以我添加了一个 QGraphics 场景,并试图在那里显示阶段.它将正确调整最后阶段的大小和居中(退出函数之前发布的最后一个阶段),但它显示中间步骤而不调整它们的大小或居中.

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 QGraphics Scene and I am trying to display the stages there. It will properly size and center the final stage (the last one posted before exiting the function), but it displays the intermediate steps without sizing or centering them.

发布图片的代码是:

        #Code to generate the image and create the loop here...
        imgQ = ImageQt.ImageQt(img)
        pixMap = QtGui.QPixmap.fromImage(imgQ)

        scene = QtGui.QGraphicsScene()
        self.ui.previewGv.setScene(scene)
        pixMap = pixMap.scaled(self.ui.previewGv.size())
        #scene.clear()
        scene.addPixmap(pixMap)               
        self.ui.previewGv.repaint()
        self.ui.previewGv.show()

有没有办法让它正确显示每个阶段?

Is there some way to get it to properly display each stage?

推荐答案

如果没有循环代码和工作示例,很难确定您的问题.但我有类似的测试应用程序,希望它会有所帮助.

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

要点:

  • 如果你在循环中做一些处理或sleep,你需要调用QCoreApplication.processEvents()来让Qt做更新.

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

我正在保存对 ImageQt.ImageQt (self.imgQ) 的引用,否则它会崩溃.

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

据我所知,您正在每次迭代中创建 QGraphicsScene,这是创建一次然后调用 scene.clear() 的更好解决方案.

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

缩放像素图只是为了显示它的大小和居中是昂贵的,QGraphicsView.fitInView() 为此目的而制作.

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

这篇关于在 QGraphicsScene 中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆