Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed
Forum Updated to NodeBB v4.3 + New Features

QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 581 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.
  • JonBJ JonB

    @gerrard87
    I'm just not sure with Python's reference counting and garbage collection whether your img.data stays in existence, as required by that QImage constructor. Why don't you try something like:

    img = QImage(width, height, QImage.Format_RGB888)  
    img.loadFromData(img.data, len(img.data))
    

    which I believe manages the data for you to see whether it makes any differenece?

    If your app crashes after a while check computer's free memory available to see if it's endlessly allocating.

    G Offline
    G Offline
    gerrard87
    wrote on last edited by
    #4

    @JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

    img.loadFromData(img.data, len(img.data))

    i get a Error when i using:

            img = QImage(img.data, width, height, step, QImage.Format_RGB888) 
            img.loadFromData(img.data, len(img.data))
    

    AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • G gerrard87

      @JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

      img.loadFromData(img.data, len(img.data))

      i get a Error when i using:

              img = QImage(img.data, width, height, step, QImage.Format_RGB888) 
              img.loadFromData(img.data, len(img.data))
      

      AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

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

      @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

      Whats wrong?

      The error message already told you what is wrong.
      Also easy to find out that there is no data property in QImage (read its docu).
      What you need is rather https://doc.qt.io/qt-5/qimage.html#bits-1

      But i don't understand what you are doing.
      You create a QImage from another one (first line), then you call loadFromData from exact same image (second line) - what is the point?

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

      1 Reply Last reply
      0
      • G gerrard87

        @JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

        img.loadFromData(img.data, len(img.data))

        i get a Error when i using:

                img = QImage(img.data, width, height, step, QImage.Format_RGB888) 
                img.loadFromData(img.data, len(img.data))
        

        AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #6

        @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

        AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

        But I copied my img.data from your img = QImage(img.data, width, height, step, QImage.Format_RGB888) . I did say "something like", you are supposed to figure it yourself....

        Oh, I see, you are using the same-named local img variable for two different types of image object! Just why Python is so meh....

        You are using img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB). So use a different variable name for the QImage now.

        G 1 Reply Last reply
        0
        • JonBJ JonB

          @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

          AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

          But I copied my img.data from your img = QImage(img.data, width, height, step, QImage.Format_RGB888) . I did say "something like", you are supposed to figure it yourself....

          Oh, I see, you are using the same-named local img variable for two different types of image object! Just why Python is so meh....

          You are using img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB). So use a different variable name for the QImage now.

          G Offline
          G Offline
          gerrard87
          wrote on last edited by
          #7

          @JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

          @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

          AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

          But I copied my img.data from your img = QImage(img.data, width, height, step, QImage.Format_RGB888) . I did say "something like", you are supposed to figure it yourself....

          Oh, I see, you are using the same-named local img variable for two different types of image object! Just why Python is so meh....

          You are using img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB). So use a different variable name for the QImage now.

          mmh so just change this?

              def cap_img(self, cap):
                  ret, img = cap.read()
                  img = cv2.flip(img,1)#flip cam
                  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                  height, width, channel = img.shape
                  step = channel * width        
                  
                  img_new= QImage(img.data, width, height, step, QImage.Format_RGB888)  
          
                  if height != HEIGHT and width != WIDTH:
                      img = img.scaled(WIDTH, HEIGHT)
                  
                  return img
          

          would that be enough? its not working because scaled is not found (object has no attribute 'scaled')

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • G gerrard87

            @JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

            @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

            AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

            But I copied my img.data from your img = QImage(img.data, width, height, step, QImage.Format_RGB888) . I did say "something like", you are supposed to figure it yourself....

            Oh, I see, you are using the same-named local img variable for two different types of image object! Just why Python is so meh....

            You are using img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB). So use a different variable name for the QImage now.

            mmh so just change this?

                def cap_img(self, cap):
                    ret, img = cap.read()
                    img = cv2.flip(img,1)#flip cam
                    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                    height, width, channel = img.shape
                    step = channel * width        
                    
                    img_new= QImage(img.data, width, height, step, QImage.Format_RGB888)  
            
                    if height != HEIGHT and width != WIDTH:
                        img = img.scaled(WIDTH, HEIGHT)
                    
                    return img
            

            would that be enough? its not working because scaled is not found (object has no attribute 'scaled')

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

                img_new= QImage(img.data, width, height, step, QImage.Format_RGB888)  
            
                if height != HEIGHT and width != WIDTH:
                    img = img.scaled(WIDTH, HEIGHT)
            

            Don't you think there's something wrong here?

            And you forgot the copy - just saying it for the third time...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • G gerrard87

              @JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

              @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

              AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?

              But I copied my img.data from your img = QImage(img.data, width, height, step, QImage.Format_RGB888) . I did say "something like", you are supposed to figure it yourself....

              Oh, I see, you are using the same-named local img variable for two different types of image object! Just why Python is so meh....

              You are using img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB). So use a different variable name for the QImage now.

              mmh so just change this?

                  def cap_img(self, cap):
                      ret, img = cap.read()
                      img = cv2.flip(img,1)#flip cam
                      img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                      height, width, channel = img.shape
                      step = channel * width        
                      
                      img_new= QImage(img.data, width, height, step, QImage.Format_RGB888)  
              
                      if height != HEIGHT and width != WIDTH:
                          img = img.scaled(WIDTH, HEIGHT)
                      
                      return img
              

              would that be enough? its not working because scaled is not found (object has no attribute 'scaled')

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #9

              @gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

              its not working because scaled is not found (object has no attribute 'scaled')

              Why do you think that is? You should understand what you are doing/changing, not just copy & paste.

              1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                I already told them on so that he simply should use QImage::copy() to make sure the image data is copied. But ...

                G Offline
                G Offline
                gerrard87
                wrote on last edited by
                #10

                @Christian-Ehrlicher said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

                I already told them on so that he simply should use QImage::copy() to make sure the image data is copied. But ...

                sorry i just started with python and im still learning. I got this code from a friend and now i must work with it.
                Can you help me how the line with Qimage::copy() must look like? Still dont know how to make it work.

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

                  Hi,

                  I would recommend that you do all image manipulation with OpenCV since you have the original data in it.

                  Then

                  return QImage(img.data, width, height, step, QImage.Format_RGB888).copy()
                  

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

                  G 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    I would recommend that you do all image manipulation with OpenCV since you have the original data in it.

                    Then

                    return QImage(img.data, width, height, step, QImage.Format_RGB888).copy()
                    
                    G Offline
                    G Offline
                    gerrard87
                    wrote on last edited by
                    #12

                    @SGaist said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:

                    return QImage(img.data, width, height, step, QImage.Format_RGB888).copy()

                    ok thank you, but what you mean with doing all the stuff with OpenCV? What kind of function in particular? I thought i was already doing everything with OpenCV?!
                    THank You all.

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

                      You are not doing the resizing with OpenCV.

                      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