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

Draw a rectangle over an image

Scheduled Pinned Locked Moved Solved Qt for Python
27 Posts 5 Posters 5.7k Views
  • 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.
  • J john_hobbyist
    3 Mar 2022, 21:32

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

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 4 Mar 2022, 05:20 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 4 Mar 2022, 07:25 last edited by john_hobbyist 3 Apr 2022, 07:28
      #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
      
      
      J 1 Reply Last reply 4 Mar 2022, 07:30
      0
      • J john_hobbyist
        4 Mar 2022, 07:25

        @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
        
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 4 Mar 2022, 07:30 last edited by jsulm 3 Apr 2022, 07:30
        #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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 4 Mar 2022, 07:31 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 4 Mar 2022, 08:22 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 4 Mar 2022, 15:04 last edited by john_hobbyist 3 Apr 2022, 15:05
              #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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 4 Mar 2022, 20:07 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 4 Mar 2022, 21:22
                0
                • S SGaist
                  4 Mar 2022, 20:07

                  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 4 Mar 2022, 21:22 last edited by john_hobbyist 3 Apr 2022, 21:31
                  #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 5 Mar 2022, 11:58 last edited by
                    #26

                    Any idea?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 5 Mar 2022, 20:24 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

                      27/27

                      5 Mar 2022, 20:24

                      • Login

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