python - 如何将场景中的 QGraphicsItems 绘制到 QImage 上而不在 QGraphicsScene 中更改它们?
问题描述
我正在将QGraphicsItem
' 转换为QGraphicsScene
. 我正在使用这些掩码对视频进行进一步处理(取掩码内的平均强度)。为了实现这一点,我将场景中的每个项目一个一个地绘制在 a 上QImage
,它的大小刚好足以包裹该项目。一切运行良好,但场景中的项目消失了。那是因为当我在QImage
. 完成后我将原来的笔放回原处,但这些物品没有重新出现在现场。如何“刷新”场景以使项目重新出现,或者防止项目完全消失?
我真的找不到任何遇到这个问题的人。所以也许我只是在做一些根本错误的事情。欢迎任何建议。
这是我的代码:
class MyThread(QtCore.QThread):
def __init__(self, scene):
super().__init__()
self.scene = scene
def run(self):
for item in self.scene.items():
# Render the ROI item to create a rasterized mask.
qimage = self.qimage_from_shape_item(item)
# do some stuff
@staticmethod
def qimage_from_shape_item(item: QtWidgets.QAbstractGraphicsShapeItem) -> QtGui.QImage:
# Get items pen and brush to set back later.
pen = item.pen()
brush = item.brush()
# Remove pen, set brush to white.
item.setPen(QtGui.QPen(QtCore.Qt.NoPen))
item.setBrush(QtCore.Qt.white)
# Determine the bounding box in pixel coordinates.
top = int(item.scenePos().y() + item.boundingRect().top())
left = int(item.scenePos().x() + item.boundingRect().left())
bottom = int(item.scenePos().y() + item.boundingRect().bottom()) + 1
right = int(item.scenePos().x() + item.boundingRect().right()) + 1
size = QtCore.QSize(right - left, bottom - top)
# Initialize qimage, use 8-bit grayscale.
qimage = QtGui.QImage(size, QtGui.QImage.Format_Grayscale8)
qimage.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(qimage)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
# Offset the painter to paint item in its correct pixel location.
painter.translate(item.scenePos().x() - left, item.scenePos().y() - top)
# Paint the item.
item.paint(painter, QtWidgets.QStyleOptionGraphicsItem())
# Set the pen and brush back.
item.setPen(pen)
item.setBrush(brush)
# Set the pixel coordinate offset of the item to the QImage.
qimage.setOffset(QtCore.QPoint(left, top))
return qimage
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(widget)
view = QtWidgets.QGraphicsView()
layout.addWidget(view)
scene = QtWidgets.QGraphicsScene()
view.setScene(scene)
thread = MyThread(scene)
view.setFixedSize(400, 300)
scene.setSceneRect(0, 0, 400, 300)
rect_item = QtWidgets.QGraphicsRectItem()
p = QtCore.QPointF(123.4, 56.78)
rect_item.setPos(p)
r = QtCore.QRectF(0., 0., 161.8, 100.)
rect_item.setRect(r)
scene.addItem(rect_item)
button = QtWidgets.QPushButton("Get masks")
layout.addWidget(button)
button.clicked.connect(thread.start)
widget.show()
sys.exit(app.exec_())
解决方案
问题是您“只能在主线程上创建和使用 GUI 小部件”,请在此处查看此 SO 答案中的更多信息。
我解决它的方法是将GUI交互部分,即qimage_from_shape_item(),从线程中取出并在主循环中处理它。我想我直接使用这些项目仍然不是很好,尽管暂时设置 NoPen 没有可见的闪烁效果或任何东西。
另一种可能是使用QGraphicsScene::render;但是,我不知道如何在不与场景中的其他项目交互的情况下逐个渲染项目。
推荐阅读
- azure - 如何将部门添加到 Azure AD 令牌
- php - Symfony 3.4 为什么 Formtype 的 CollectionType 不发布值
- c - 当流不为 0 时 fgetc() 返回 EOF
- javascript - 在java脚本中循环有限的对象
- javascript - 需要为我的简单 ToDoList 添加一个 localStorage
- javascript - 在 Django 中搜索 javascript
- sql - 比较表中的字符串值
- heroku - 在 Heroku 上对 github 进行身份验证时无法连接到 GitHub
- java - java中打开的文件太多
- php - 为什么带有重定向的 add_filter 会导致无限重定向循环?