Python 在Qgraphicscene中的图像中显示
我有一个简短的脚本,可以用PIL修改图像几次。我想能够显示中间步骤,因为它与他们结束,所以我添加了一个QGraphics场景,我试图显示那里的阶段。它将正确调整最终阶段的大小并居中(退出功能之前发布的最后一个阶段),但它显示中间步骤,而不调整大小或居中 发布图像的代码为:Python 在Qgraphicscene中的图像中显示,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,我有一个简短的脚本,可以用PIL修改图像几次。我想能够显示中间步骤,因为它与他们结束,所以我添加了一个QGraphics场景,我试图显示那里的阶段。它将正确调整最终阶段的大小并居中(退出功能之前发布的最后一个阶段),但它显示中间步骤,而不调整大小或居中 发布图像的代码为: #Code to generate the image and create the loop here... imgQ = ImageQt.ImageQt(img) pixMa
#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()
有没有办法让它正确显示每个阶段?如果没有循环代码和工作示例,很难确定您的问题。但我有类似的测试应用程序,希望能有所帮助
#!/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_())
要点:
- 如果您正在循环中执行某些处理或睡眠,则需要调用
以允许Qt执行更新QCoreApplication.processEvents()
- 我正在保存对
(ImageQt.ImageQt
)的引用,否则它将崩溃self.imgQ
- 据我所知,在每次迭代中都要创建
,最好的解决方案是创建一次,然后调用qgraphicscene
scene.clear()
- 缩放pixmap以显示其大小和居中是非常昂贵的,
QGraphicsView.fitInView()