我建议使用类
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_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)