我在QHBoxLayout
打开了一张图片。我需要裁剪打开的图像并保存裁剪的图像。我怎么能在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()
答案 0 :(得分:5)
我建议使用课程QtGui.QRubberBand
来选择要裁剪的图像区域。 (PySide aslo实现相同的PyQt)
首先,实施方法mouseMoveEvent (self, QMouseEvent)
,mouseReleaseEvent (self, QMouseEvent)
和mousePressEvent (self, QMouseEvent)
(更多信息请参阅QtGui.QRubberBand
类参考)。
接下来,使用QRect QWidget.geometry (self)
获取QtGui.QRubberBand
的最后几何图形以裁剪图像。
最后,使用QPixmap QPixmap.copy (self, QRect rect = QRect())
通过从裁剪区域放置几何图形来裁剪图像。并使用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_())
答案 1 :(得分:1)
我会使用QImage的copy
方法:
im2 = im.copy(self.rect)
im2.save(...)
答案 2 :(得分:-1)
import sys
from PyQt5 import QtGui, QtCore,QtWidgets
from PyQt5.QtWidgets import QRubberBand, QLabel, QApplication, QWidget
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QRect
class QExampleLabel (QLabel):
def __init__(self, parentQWidget = None):
super(QExampleLabel, self).__init__(parentQWidget)
self.initUI()
def initUI (self):
self.setPixmap(QPixmap('input.png'))
def mousePressEvent (self, eventQMouseEvent):
self.originQPoint = eventQMouseEvent.pos()
self.currentQRubberBand = QRubberBand(QRubberBand.Rectangle, self)
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, QtCore.QSize()))
self.currentQRubberBand.show()
def mouseMoveEvent (self, eventQMouseEvent):
self.currentQRubberBand.setGeometry(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 = QApplication(sys.argv)
myQExampleLabel = QExampleLabel()
myQExampleLabel.show()
sys.exit(myQApplication.exec_())