如何裁剪图像并保存?

如何裁剪图像并保存?,第1张

如何裁剪图像并保存?

我建议使用类

QtGui.QRubberBand
来选择要裁剪的图像区域。(PySide还实现了与PyQt相同的功能)

首先,实施方法

mouseMoveEvent (self, QMouseEvent)
mouseReleaseEvent (self,QMouseEvent)
mousePressEvent (self,QMouseEvent)
(更多信息在读
QtGui.QRubberBand
类参考)。

接下来,使用获取

QtGui.QRubberBand
裁剪图像的最后几何形状
QRect QWidget.geometry(self)

最后,用于

QPixmap QPixmap.copy (self, QRect rect =QRect())
通过放置裁剪区域中的几何来裁剪图像。并通过使用保存图像
boolQPixmap.save (self, QString fileName, str format = None, int quality =-1)

例;

import sysfrom PyQt4 import QtGui, QtCoreclass 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_())


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存