How to crop a image and save?

curiousguy

I have opened a image in a QHBoxLayout. I need to crop the opened image and save the cropped image. How I can do this in 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()
Kitsune Meyoko

I suggest use class QtGui.QRubberBand to select area of image to crop. (PySide also implements the same functionality as PyQt)

First, implement method mouseMoveEvent (self, QMouseEvent), mouseReleaseEvent (self, QMouseEvent) and mousePressEvent (self, QMouseEvent) (More infomation read in QtGui.QRubberBand class reference).

Next, get last geometry of QtGui.QRubberBand to crop image by use QRect QWidget.geometry (self).

Last, Use QPixmap QPixmap.copy (self, QRect rect = QRect()) to crop image by put geometry from crop area. And save image it by use bool QPixmap.save (self, QString fileName, str format = None, int quality = -1).

Example;

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to select a portion of an image, crop, and save it using Swift?

How to crop any selected area of an image and save it to the server?

How to get a image from gallery, crop it and save it in app

How to crop image with css

How to crop image into a square

In NiFi how to crop image?

How to crop image or rectangle

How to crop and save a RenderTargetBitmap UWP

how to crop a image , resize croper frame and save it in another canvas by using angular 2 and typescript

MATLAB - How can I crop some part of an image from .fig file and save it as .mat?

How to resize then crop an image with canvas

How to crop an image into a circle with java?

How to scale, rotate and crop an Image

How to automatically crop and center an image

How to use mogrify to crop an image

How to crop image to text in Powerpoint?

How to crop an SOIL loaded image

How to center crop an image in SwiftUI

How to crop an image and set to imageVeiw

how to get an image to crop in center

How to crop image manually with paperclip?

How to crop image diagonally in canvas

How to crop an image width svg?

How to crop image from camera preview (surface view) and save it instead of actual image captured bt camera like instagram app?

how to save/crop detected faces in dlib python

How crop image fit to content of image?

Trouble using python PIL library to crop and save image

My goal is to crop the image with a rectangle and save the crop in grayscale, but I can't understand the error in the code

how to stretch image vertically to 100% and crop horizontally