How To Render A Part Of QGraphicsScene And Save It As Image File PyQt5
Suppose I have QGraphicsPixmapItem from loaded image which is added to QGraphicsScene. And suppose I'll add several QGraphicsPolygonItem's on scene. How I can render a part of the
Solution 1:
Untested, but using QGraphicsScene::render
you should be able to do something like...
def _save_image(self):
# Get region of scene to capture from somewhere.
area = get_QRect_to_capture_from_somewhere()
# Create a QImage to render to and fix up a QPainter for it.
image = QImage(area.size(), QImage.Format_ARGB32_Premultiplied)
painter = QPainter(image)
# Render the region of interest to the QImage.
self.scene.render(painter, image.rect(), area)
painter.end()
# Save the image to a file.
image.save("capture.png")
Solution 2:
I wanted to add that render requires the QRectF object for the image.area() and area parameters, otherwise it throws a TypeError.
I did:
QtCore.QRectF(image.rect())
and created a QRectF object for the area parameter.
Post a Comment for "How To Render A Part Of QGraphicsScene And Save It As Image File PyQt5"