栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何裁剪图像并保存?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何裁剪图像并保存?

我建议使用类

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/653348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号