Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Draw a rectangle over an image
Forum Updated to NodeBB v4.3 + New Features

Draw a rectangle over an image

Scheduled Pinned Locked Moved Solved Qt for Python
27 Posts 5 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    And that code runs without triggering an exception ?
    "photo" is unknown in your paintEvent. You should draw "self.image".

    J Offline
    J Offline
    john_hobbyist
    wrote on last edited by
    #17

    @SGaist If you mean: "painter.drawImage(self.image)" I see neither the image nor the rectangle and many errors...

    jsulmJ 1 Reply Last reply
    0
    • J john_hobbyist

      @SGaist If you mean: "painter.drawImage(self.image)" I see neither the image nor the rectangle and many errors...

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #18

      @john_hobbyist said in Draw a rectangle over an image:

      many errors

      Would be nice if you would let us know what errors...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        john_hobbyist
        wrote on last edited by john_hobbyist
        #19

        @jsulm Yes, of course! The code is this (for source see my previous messages...):

        from PyQt5 import QtGui
        import sys
        from PyQt5.QtWidgets import *
        from PyQt5.QtCore import *
        from PyQt5.QtGui import *
         
        class Canvas(QWidget): 
            def __init__(self, photo, *args, **kwargs): 
                super().__init__(*args, **kwargs) 
                
                self.image = QImage(photo)
                self.setFixedSize(self.image.width(), self.image.height())
                self.pressed = self.moving = False
                self.revisions = []
         
                self.title = "PyQt5 Drawing Rectangle"
                self.top = 100
                self.left = 100
                self.width = 680
                self.height = 500
         
                self.InitWindow()
         
            def InitWindow(self):
                self.setWindowIcon(QtGui.QIcon("icon.png"))
                self.setWindowTitle(self.title)
                self.setGeometry(self.top, self.left, self.width, self.height)
                self.show()
        
            def paintEvent(self, e):
                painter = QPainter(self)
                painter.drawImage(self.image)
                painter.setPen(QPen(Qt.green, 5, Qt.SolidLine))
                painter.setBrush(QBrush(Qt.transparent))
                painter.drawRect(100, 150, 400,200)
        
        class MainWindow(QMainWindow):
        
            def __init__(self):
                super().__init__()
                w = QWidget()
                self.setCentralWidget(w)
                canvas = Canvas('image3.png') 
                grid = QGridLayout(w)
                grid.addWidget(canvas)
        
        if __name__ == '__main__':
            app = QApplication(sys.argv)
            gui = MainWindow()
            gui.show()
            sys.exit(app.exec_())
        

        The errors are these:

        Traceback (most recent call last):
          File "draw_fixed_rectangle_with_image.py", line 32, in paintEvent
            painter.drawImage(self.image)
        TypeError: arguments did not match any overloaded call:
          drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, QRect, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, Union[QPointF, QPoint], QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, QPoint, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, int, int, QImage, sx: int = 0, sy: int = 0, sw: int = -1, sh: int = -1, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QRectF, QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QRect, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, Union[QPointF, QPoint], QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QPoint, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
        QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
        Traceback (most recent call last):
          File "draw_fixed_rectangle_with_image.py", line 32, in paintEvent
            painter.drawImage(self.image)
        TypeError: arguments did not match any overloaded call:
          drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, QRect, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, Union[QPointF, QPoint], QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, QPoint, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, int, int, QImage, sx: int = 0, sy: int = 0, sw: int = -1, sh: int = -1, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QRectF, QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QRect, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, Union[QPointF, QPoint], QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QPoint, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
        Traceback (most recent call last):
          File "draw_fixed_rectangle_with_image.py", line 32, in paintEvent
            painter.drawImage(self.image)
        TypeError: arguments did not match any overloaded call:
          drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, QRect, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, Union[QPointF, QPoint], QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, QPoint, QImage): argument 1 has unexpected type 'QImage'
          drawImage(self, int, int, QImage, sx: int = 0, sy: int = 0, sw: int = -1, sh: int = -1, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QRectF, QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QRect, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, Union[QPointF, QPoint], QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          drawImage(self, QPoint, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
        QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
        QPaintDevice: Cannot destroy paint device that is being painted
        
        
        jsulmJ 1 Reply Last reply
        0
        • J john_hobbyist

          @jsulm Yes, of course! The code is this (for source see my previous messages...):

          from PyQt5 import QtGui
          import sys
          from PyQt5.QtWidgets import *
          from PyQt5.QtCore import *
          from PyQt5.QtGui import *
           
          class Canvas(QWidget): 
              def __init__(self, photo, *args, **kwargs): 
                  super().__init__(*args, **kwargs) 
                  
                  self.image = QImage(photo)
                  self.setFixedSize(self.image.width(), self.image.height())
                  self.pressed = self.moving = False
                  self.revisions = []
           
                  self.title = "PyQt5 Drawing Rectangle"
                  self.top = 100
                  self.left = 100
                  self.width = 680
                  self.height = 500
           
                  self.InitWindow()
           
              def InitWindow(self):
                  self.setWindowIcon(QtGui.QIcon("icon.png"))
                  self.setWindowTitle(self.title)
                  self.setGeometry(self.top, self.left, self.width, self.height)
                  self.show()
          
              def paintEvent(self, e):
                  painter = QPainter(self)
                  painter.drawImage(self.image)
                  painter.setPen(QPen(Qt.green, 5, Qt.SolidLine))
                  painter.setBrush(QBrush(Qt.transparent))
                  painter.drawRect(100, 150, 400,200)
          
          class MainWindow(QMainWindow):
          
              def __init__(self):
                  super().__init__()
                  w = QWidget()
                  self.setCentralWidget(w)
                  canvas = Canvas('image3.png') 
                  grid = QGridLayout(w)
                  grid.addWidget(canvas)
          
          if __name__ == '__main__':
              app = QApplication(sys.argv)
              gui = MainWindow()
              gui.show()
              sys.exit(app.exec_())
          

          The errors are these:

          Traceback (most recent call last):
            File "draw_fixed_rectangle_with_image.py", line 32, in paintEvent
              painter.drawImage(self.image)
          TypeError: arguments did not match any overloaded call:
            drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, QRect, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, Union[QPointF, QPoint], QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, QPoint, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, int, int, QImage, sx: int = 0, sy: int = 0, sw: int = -1, sh: int = -1, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QRectF, QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QRect, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, Union[QPointF, QPoint], QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QPoint, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
          Traceback (most recent call last):
            File "draw_fixed_rectangle_with_image.py", line 32, in paintEvent
              painter.drawImage(self.image)
          TypeError: arguments did not match any overloaded call:
            drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, QRect, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, Union[QPointF, QPoint], QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, QPoint, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, int, int, QImage, sx: int = 0, sy: int = 0, sw: int = -1, sh: int = -1, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QRectF, QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QRect, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, Union[QPointF, QPoint], QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QPoint, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          Traceback (most recent call last):
            File "draw_fixed_rectangle_with_image.py", line 32, in paintEvent
              painter.drawImage(self.image)
          TypeError: arguments did not match any overloaded call:
            drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, QRect, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, Union[QPointF, QPoint], QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, QPoint, QImage): argument 1 has unexpected type 'QImage'
            drawImage(self, int, int, QImage, sx: int = 0, sy: int = 0, sw: int = -1, sh: int = -1, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QRectF, QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QRect, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, Union[QPointF, QPoint], QImage, QRectF, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
            drawImage(self, QPoint, QImage, QRect, flags: Union[Qt.ImageConversionFlags, Qt.ImageConversionFlag] = Qt.ImageConversionFlag.AutoColor): argument 1 has unexpected type 'QImage'
          QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
          QPaintDevice: Cannot destroy paint device that is being painted
          
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #20

          @john_hobbyist Please take a look at documentation: https://doc.qt.io/qt-5/qpainter.html
          What are drawImage() parameters? And what do you pass to it? It is quite apparent what you are doing wrong even from the error message itself:

          drawImage(self, QRectF, QImage): argument 1 has unexpected type 'QImage'
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #21

            The error is pretty explicit. There's no overload of drawImage that takes only a QImage as parameter. It's always the second one.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • J Offline
              J Offline
              john_hobbyist
              wrote on last edited by
              #22

              Thank you all for your time and your significant comments! I mark it as "Solved!"

              1 Reply Last reply
              0
              • J Offline
                J Offline
                john_hobbyist
                wrote on last edited by john_hobbyist
                #23

                Just a small question, in case I want to depict the rectangle in an image displayed in this application: https://github.com/Axel-Erfurt/OrthoViewLite/blob/main/OrthoViewLite.py how can I achieve that? Where should I place the above code?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #24

                  What is this rectangle supposed to do in your application ? Depending on that, it would be simpler to use a QRubberBand.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What is this rectangle supposed to do in your application ? Depending on that, it would be simpler to use a QRubberBand.

                    J Offline
                    J Offline
                    john_hobbyist
                    wrote on last edited by john_hobbyist
                    #25

                    @SGaist The rectangles are placed there in specific positions and with specific dimensions....They are programmed from the python code. I don't draw rectangles with the mouse...

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      john_hobbyist
                      wrote on last edited by
                      #26

                      Any idea?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #27

                        Well, it would be simpler to draw your rectangle directly on the QImage where you create them.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved