Let's say I have a QPixmap
loaded in a QGraphicsView
that has been scaled by a factor of 2, so that the image is now clipped on all sides. I would like to take the clipped image and save it as a new image file so that it matches what is seen in the QGraphicsView
. I know that QPixmap
has a save() function but without reapplying the view transform it won't differ from the original.
How can this be accomplished? Thank you.
The viewport()
is the widget that is displayed visually in the QGraphicsView
, so you must save an image of that widget with render()
.
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
view = QtGui.QGraphicsView()
scene = QtGui.QGraphicsScene()
view.setScene(scene)
scene.addPixmap(QtGui.QPixmap("/path/of/image"))
view.scale(4, 4)
view.show()
pixmap = QtGui.QPixmap(view.viewport().size())
view.viewport().render(pixmap)
pixmap.save("test.png")
sys.exit(app.exec_())