本文介紹了如何渲染 QGraphicsScene 的一部分并將其保存為圖像文件 PyQt5的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
假設我有來自加載圖像的 QGraphicsPixmapItem
,它被添加到 QGraphicsScene
.假設我將在場景中添加幾個 QGraphicsPolygonItem
.如何將場景的一部分渲染為全尺寸圖像,同時使用不在空白區域中的多邊形和保存該區域作為圖像文件.強>
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 scene as full-size image both with polygons that are not in blank area and save this area as image file.
class ImageView(QtWidgets.QGraphicsView):
def __init__(self, parent):
super(ImageView, self).__init__(parent)
self.setFocus()
self._zoom = 0
self._empty = True
self.scene = QtWidgets.QGraphicsScene(self)
self._image = QGraphicsPixmapItem()
self.scene.addItem(self._image)
self.setScene(self.scene)
# some other actions
foo()
def fitInView(self):
# custom fit in view and scaling
bar()
# some other methods
class MainWindow(QtWidgets.QWidget):
def __init__(self):
self.viewer = ImageView(self)
foo()
def _save_image(self):
# method that I need to implement
pass
推薦答案
未經測試,但使用 QGraphicsScene::render
你應該可以做類似...
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")
這篇關于如何渲染 QGraphicsScene 的一部分并將其保存為圖像文件 PyQt5的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!