Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to draw rectangle on image?



  • I want to draw rectangle on image who I choose. But I dont know how. Please help me : (

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QFileDialog
    from PyQt5.QtGui import QPixmap, QImage, QPen, QPainter
    from PyQt5.QtCore import Qt
    from alg import SpriteSlicer
    import cv2
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 700)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.btn_Open = QtWidgets.QPushButton(self.centralwidget)
            self.btn_Open.setGeometry(QtCore.QRect(20, 10, 94, 40))
            self.btn_Open.setObjectName("btn_Open")
            self.btn_Save = QtWidgets.QPushButton(self.centralwidget)
            self.btn_Save.setGeometry(QtCore.QRect(160, 10, 94, 40))
            self.btn_Save.setObjectName("btn_Save")
            self.btn_Start = QtWidgets.QPushButton(self.centralwidget)
            self.btn_Start.setGeometry(QtCore.QRect(350, 10, 94, 40))
            self.btn_Start.setObjectName("btn_Start")
            self.imageView = QtWidgets.QLabel(self.centralwidget)
            self.imageView.setGeometry(QtCore.QRect(0, 55, 800, 600))
            self.imageView.setStyleSheet(u"background-color: rgb(115, 115, 115);")
            self.imageView.setText("")
            self.imageView.setObjectName("imageView")
            MainWindow.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
            self.file_name = None
    
            self.btn_Open.clicked.connect(self.open_file)
            self.btn_Start.clicked.connect(self.start_alg)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.btn_Open.setText(_translate("MainWindow", "Open"))
            self.btn_Save.setText(_translate("MainWindow", "Save"))
            self.btn_Start.setText(_translate("MainWindow", "Start"))
    
        def open_file(self):
            self.file_name, _ = QFileDialog.getOpenFileName(MainWindow, "", "", "Image files (*.jpg *.png)")
            self.image = QPixmap(self.file_name).scaled(800, 600, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            self.w = self.image.width()
            self.h = self.image.height()
            self.imageView.setPixmap(self.image)
    
        def start_alg(self):
            if self.file_name is not None:
                sprite_slicer = SpriteSlicer(self.file_name, self.w, self.h)
                self.coords = sprite_slicer.rects_coords #this is the coords I want to draw on image
    
        def paintEvent(self, event):
            pen = QPen()
            pen.setWidth(5)
            print("WORK")
            painter = QPainter(MainWindow)
            painter.drawPixmap(self.rect(), self.image)
            painter.setPen(pen)
            painter.drawRect(300, 300, 500, 500)
            self.imageView.setPixmap(self.image)
    
    #,Qt.SmoothTransformation
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    
    

  • Lifetime Qt Champion

    Hi,

    You are adding QWidget methods to an object based class.

    Create a subclass of QMainWindow where you add these methods.

    @Toso said in How to draw rectangle on image?:

    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)

    Use that in your QMainWindow __init__ function.



  • isnt working, draw is not on image

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QFileDialog, QWidget, QApplication
    from PyQt5.QtGui import QPixmap, QImage, QPen, QPainter
    from PyQt5.QtCore import Qt
    from alg import SpriteSlicer
    import cv2
    
    class GUI_SpriteSlicer(QWidget):
    
        def __init__(self):
            super().__init__()
    
            self.resize(800, 700)
            self.centralwidget = QtWidgets.QWidget(self)
            self.centralwidget.setObjectName("centralwidget")
            self.btn_Open = QtWidgets.QPushButton(self.centralwidget)
            self.btn_Open.setGeometry(QtCore.QRect(20, 10, 94, 40))
            self.btn_Open.setObjectName("btn_Open")
            self.btn_Save = QtWidgets.QPushButton(self.centralwidget)
            self.btn_Save.setGeometry(QtCore.QRect(160, 10, 94, 40))
            self.btn_Save.setObjectName("btn_Save")
            self.btn_Start = QtWidgets.QPushButton(self.centralwidget)
            self.btn_Start.setGeometry(QtCore.QRect(350, 10, 94, 40))
            self.btn_Start.setObjectName("btn_Start")
            self.imageView = QtWidgets.QLabel(self.centralwidget)
            self.imageView.setGeometry(QtCore.QRect(0, 55, 800, 600))
            self.imageView.setStyleSheet(u"background-color: rgb(115, 115, 115);")
            self.imageView.setText("")
            self.imageView.setObjectName("imageView")
            #self.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(self)
            QtCore.QMetaObject.connectSlotsByName(self)
    
            self.file_name = None
            self.draw = False
            self.btn_Open.clicked.connect(self.open_file)
            self.btn_Start.clicked.connect(self.start_alg)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.btn_Open.setText(_translate("MainWindow", "Open"))
            self.btn_Save.setText(_translate("MainWindow", "Save"))
            self.btn_Start.setText(_translate("MainWindow", "Start"))
    
        def open_file(self):
            self.file_name, _ = QFileDialog.getOpenFileName(self, "", "", "Image files (*.jpg *.png)")
            self.image = QPixmap(self.file_name).scaled(800, 600, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            self.w = self.image.width()
            self.h = self.image.height()
            self.imageView.setPixmap(self.image)
            self.imageView.setAlignment(Qt.AlignCenter)
    
        def start_alg(self):
            if self.file_name is not None:
                sprite_slicer = SpriteSlicer(self.file_name, self.w, self.h)
                self.coords = sprite_slicer.rects_coords
                self.draw = True
                #img = sprite_slicer.original
                #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                #image = QImage(img, self.w, self.h, img.strides[0], QImage.Format_RGB888)
                #self.imageView.setPixmap(QPixmap.fromImage(image))
    
        def paintEvent(self, event):
            if self.draw:
                print("TRUE")
    
                pen = QPen()
                pen.setWidth(5)
    
                painter = QPainter(self.imageView)
                painter.setPen(pen)
                painter.drawEllipse(200, 200, 500, 500)
                self.update()
                self.draw = False
    
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv)
        gui = GUI_SpriteSlicer()
        gui.show()
    
        sys.exit(app.exec_())
    

  • Lifetime Qt Champion

    @Toso said in How to draw rectangle on image?:

    painter = QPainter(self.imageView)

    You cannot paint on a different widget hence the error message you get.


Log in to reply