如何裁剪图片并保存?

0 投票
3 回答
7729 浏览
提问于 2025-04-21 04:23

我在一个 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()

3 个回答

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

当然可以!请把你想要翻译的内容发给我,我会帮你把它变得更简单易懂。

1

我会使用QImage的copy方法:

im2 = im.copy(self.rect)
im2.save(...)
8

我建议使用类 QtGui.QRubberBand 来选择要裁剪的图片区域。(PySide 也有和 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_())

撰写回答