How to crop a image and save?

2019-03-22 08:53发布

问题:

I have opened a image in a QHBoxLayout. I need to crop the opened image and save the cropped image. How I can do this in PySide?

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        hbox = QtGui.QHBoxLayout(self)
        pixmap = QtGui.QPixmap("re.png")

        lbl = QtGui.QLabel(self)
        lbl.setPixmap(pixmap)


        self.rect = QtCore.QRect()


        hbox.addWidget(lbl)
        self.setLayout(hbox)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Open Image')
        self.show()   
        # Tried here to implement Qpen      
        #self.painter = QtGui.QPainter(self)    
        #self.painter.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine));
        #self.painter.drawRect(self.rect);
def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

回答1:

I suggest use class QtGui.QRubberBand to select area of image to crop. (PySide aslo implement same PyQt)

First, implement method mouseMoveEvent (self, QMouseEvent), mouseReleaseEvent (self, QMouseEvent) and mousePressEvent (self, QMouseEvent) (More infomation read in QtGui.QRubberBand class reference).

Next, get last geometry of QtGui.QRubberBand to crop image by use QRect QWidget.geometry (self).

Last, Use QPixmap QPixmap.copy (self, QRect rect = QRect()) to crop image by put geometry from crop area. And save image it by use bool QPixmap.save (self, QString fileName, str format = None, int quality = -1).

Example;

import sys
from PyQt4 import QtGui, QtCore

class QExampleLabel (QtGui.QLabel):
    def __init__(self, parentQWidget = None):
        super(QExampleLabel, self).__init__(parentQWidget)
        self.initUI()

    def initUI (self):
        self.setPixmap(QtGui.QPixmap('input.png'))

    def mousePressEvent (self, eventQMouseEvent):
        self.originQPoint = eventQMouseEvent.pos()
        self.currentQRubberBand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
        self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, QtCore.QSize()))
        self.currentQRubberBand.show()

    def mouseMoveEvent (self, eventQMouseEvent):
        self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())

    def mouseReleaseEvent (self, eventQMouseEvent):
        self.currentQRubberBand.hide()
        currentQRect = self.currentQRubberBand.geometry()
        self.currentQRubberBand.deleteLater()
        cropQPixmap = self.pixmap().copy(currentQRect)
        cropQPixmap.save('output.png')

if __name__ == '__main__':
    myQApplication = QtGui.QApplication(sys.argv)
    myQExampleLabel = QExampleLabel()
    myQExampleLabel.show()
    sys.exit(myQApplication.exec_())


回答2:

I would use QImage's copy method:

im2 = im.copy(self.rect)
im2.save(...)