I have QGraphicsPixmapItem
from loaded image(6000x4000 pixel) which is added to QGraphicsScene
. I added several filled square and using addRect
on scene.
How I can save edited image with a high resolution as full-size image
Here is my code
class PhotoViewer(QGraphicsView):
def __init__(self, parent):
self._zoom = 0
self._items = []
self._scene = QGraphicsScene(self)
self._photo = QGraphicsPixmapItem()
self._scene.addItem(self._photo)
self.setScene(self._scene)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
self.setFrameShape(QFrame.NoFrame)
# some other actions
def fitInView(self, scale=True):
rect = QRectF(self._photo.pixmap().rect())
if not rect.isNull():
self.setSceneRect(rect)
# custom fit in view and scaling
def setPhoto(self, pixmap=None):
self._zoom = 0
if pixmap and not pixmap.isNull():
self._empty = False
self._photo.setPixmap(pixmap)
self.fitInView()
def add_a_Rectangle(self):
self._added = self._scene.addRect(rect,pen)
# custom other code
def save_image(self):
#====method that I need to implement=======#
pass
class MainWindow(QMainWindow):
def __init__(self):
self.viewer_edit = PhotoViewer(self)
def save_img(self):
self.viewer_edit.save_img()
pass
And I tried two ways, But It's not working
First
img = QPixmap(self._scene.grab())
painter = QPainter(img)
painter.setRenderHint(QPainter.Antialiasing)
self._scene.render(painter)
painter.end()
img.save('D:\\test.png')
Result
loaded image's size is 3000x3000 pixel, but Result image's size is 1881x768 pixel.
Second
I used to refer to How to render a part of QGraphicsScene and save It as image file PyQt5 and eyllanesc's comment.
w = self._photo.boundingRect().width()
h = self._photo.boundingRect().height()
image = QImage(int(w),int(h), QImage.Format_ARGB32_Premultiplied)
painter = QPainter(image)
self._scene.render(painter,QRectF(0, 0,w,h), self._photo.boundingRect())
painter.end()
image.save('D:\\test2.png',quality=100)
Finally I solved the problem