如何裁剪图像并保存? [英] How to crop a image and save?

查看:201
本文介绍了如何裁剪图像并保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 QHBoxLayout 中打开了一个图像。我需要裁剪打开的图像并保存裁剪后的图像。我如何在PySide中做到这一点?

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()


推荐答案

我建议使用类 QtGui.QRubberBand 选择要裁剪的图像区域。 (PySide还实现了与PyQt相同的功能)

I suggest use class QtGui.QRubberBand to select area of image to crop. (PySide also implements the same functionality as PyQt)

首先,实现方法 mouseMoveEvent(自己,QMouseEvent) mouseReleaseEvent(自身,QMouseEvent) mousePressEvent(自身,QMouseEvent)(更多信息请参见 QtGui .QRubberBand 类参考)。

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

接下来,获取 QtGui.QRubberBand 的最后一个几何使用 QRect QWidget.geometry(自身)来裁剪图像

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

最后,使用> $code> QPixmap QPixmap.copy(自身,QRect rect = QRect()) 通过从作物面积。并使用 bool QPixmap.save(self ,QString fileName,str格式= None,int quality = -1)

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).

示例;

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_())

这篇关于如何裁剪图像并保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆