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. PyQt QImage cna't copy, crash without error code
Forum Updated to NodeBB v4.3 + New Features

PyQt QImage cna't copy, crash without error code

Scheduled Pinned Locked Moved Solved Qt for Python
27 Posts 5 Posters 4.2k 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.
  • D darrenleeleelee1

    @J-Hilk said in PyQt QImage cna't copy, crash without error code:

    curiosity

    Yes,it also crash.

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

    @darrenleeleelee1 Did you check whether the image is a null image as I already suggested?

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

    JonBJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @darrenleeleelee1 Did you check whether the image is a null image as I already suggested?

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

      @jsulm
      He claims

      And I try to use qimage.isNull() it return False.

      assuming this check is done in the right place.

      jsulmJ 1 Reply Last reply
      2
      • JonBJ JonB

        @jsulm
        He claims

        And I try to use qimage.isNull() it return False.

        assuming this check is done in the right place.

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

        @JonB I need to read more carefully sometimes :-)

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

        1 Reply Last reply
        1
        • JonBJ JonB

          @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

              qimage = qimage.copy()
              # qimage = qimage.copy(QtCore.QRect(0, 0, 2048, 2048)) this will not crash
          

          So do you want to tell us what qImage.width() & qImage.height() of the image actually are?

          D Offline
          D Offline
          darrenleeleelee1
          wrote on last edited by
          #10

          @JonB 1767f8ea-cdc2-4a02-8f12-b6034f2aac2e-image.png
          and it return 2235 3408
          @J-Hilk I copy to tmp and it still crash.

          JonBJ 1 Reply Last reply
          0
          • D darrenleeleelee1

            @JonB 1767f8ea-cdc2-4a02-8f12-b6034f2aac2e-image.png
            and it return 2235 3408
            @J-Hilk I copy to tmp and it still crash.

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

            @darrenleeleelee1
            Then OOI how does

            qimage = qimage.copy(QtCore.QRect(0, 0, 2235, 3408))
            

            behave?

            D 1 Reply Last reply
            0
            • JonBJ JonB

              @darrenleeleelee1
              Then OOI how does

              qimage = qimage.copy(QtCore.QRect(0, 0, 2235, 3408))
              

              behave?

              D Offline
              D Offline
              darrenleeleelee1
              wrote on last edited by darrenleeleelee1
              #12

              @JonB It won't crash!But when I change to

              qimage.copy(0, 0, qimage.width(), qimage.height()) 
              

              then it crash.Even I give to other variable first it still not work

              wt = qimage.width()
              ht = qimage.height()
              qimage = qimage.copy(0, 0, wt, ht)
              
              J.HilkJ KroMignonK JonBJ 3 Replies Last reply
              0
              • D darrenleeleelee1

                @JonB It won't crash!But when I change to

                qimage.copy(0, 0, qimage.width(), qimage.height()) 
                

                then it crash.Even I give to other variable first it still not work

                wt = qimage.width()
                ht = qimage.height()
                qimage = qimage.copy(0, 0, wt, ht)
                
                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #13

                @darrenleeleelee1 what about
                qimage = qimage.copy(QtCore.QRect())


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                D 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @darrenleeleelee1 what about
                  qimage = qimage.copy(QtCore.QRect())

                  D Offline
                  D Offline
                  darrenleeleelee1
                  wrote on last edited by
                  #14

                  @J-Hilk I try it.But also crash.

                  1 Reply Last reply
                  0
                  • D darrenleeleelee1

                    @JonB It won't crash!But when I change to

                    qimage.copy(0, 0, qimage.width(), qimage.height()) 
                    

                    then it crash.Even I give to other variable first it still not work

                    wt = qimage.width()
                    ht = qimage.height()
                    qimage = qimage.copy(0, 0, wt, ht)
                    
                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by KroMignon
                    #15

                    @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                    qimage.copy(0, 0, qimage.width(), qimage.height())

                    then it crash

                    It looks very strange to me, because when looking for QImage::copy(int x, int y, int w, int h), I found:

                       inline QImage copy(int x, int y, int w, int h) const
                            { return copy(QRect(x, y, w, h)); }
                    
                    

                    What if when you change your code to:

                    qimage = QtGui.QImage(arr, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16)
                    qimageCopy = qimage.copy()
                    

                    Does this also crash?

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    0
                    • D darrenleeleelee1

                      @JonB It won't crash!But when I change to

                      qimage.copy(0, 0, qimage.width(), qimage.height()) 
                      

                      then it crash.Even I give to other variable first it still not work

                      wt = qimage.width()
                      ht = qimage.height()
                      qimage = qimage.copy(0, 0, wt, ht)
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #16

                      @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                      @JonB It won't crash!But when I change to
                      qimage.copy(0, 0, qimage.width(), qimage.height())

                      then it crash.Even I give to other variable first it still not work
                      wt = qimage.width()
                      ht = qimage.height()
                      qimage = qimage.copy(0, 0, wt, ht)

                      This all implies that the source qimage you start with is the issue.

                      Start by looking at exactly what your arr = ds.pixel_array holds/returns. The QImage() constructor you use states "it must remain valid". That looks to me like the danger area. I think when you copy a portion of the image Qt takes a copy of its own. But probably not when you copy the whole thing. For example, are you able to do a Python copy() on it so you have your own separate copy, which you then use for the image? And btw, if you have any threading involved anywhere, please say so......

                      D 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                        @JonB It won't crash!But when I change to
                        qimage.copy(0, 0, qimage.width(), qimage.height())

                        then it crash.Even I give to other variable first it still not work
                        wt = qimage.width()
                        ht = qimage.height()
                        qimage = qimage.copy(0, 0, wt, ht)

                        This all implies that the source qimage you start with is the issue.

                        Start by looking at exactly what your arr = ds.pixel_array holds/returns. The QImage() constructor you use states "it must remain valid". That looks to me like the danger area. I think when you copy a portion of the image Qt takes a copy of its own. But probably not when you copy the whole thing. For example, are you able to do a Python copy() on it so you have your own separate copy, which you then use for the image? And btw, if you have any threading involved anywhere, please say so......

                        D Offline
                        D Offline
                        darrenleeleelee1
                        wrote on last edited by
                        #17

                        @JonB Sorry I am not really get what did you mean.And I think I don't have any threading.(I think..)

                        JonBJ 1 Reply Last reply
                        0
                        • D darrenleeleelee1

                          @JonB Sorry I am not really get what did you mean.And I think I don't have any threading.(I think..)

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

                          @darrenleeleelee1
                          I have no idea what is behind your

                                  ds = dcmread('./5F329172_20170623_CR_2_1_1')
                                  arr = ds.pixel_array
                          

                          It's some external library stuff. (And btw I have no idea whether arr.shape[1], arr.shape[0] is the right thing to use. Don't bother to explain, just make sure it's correct for whatever this ds stuff is.)

                          Try this:

                          import copy
                          ...
                                  arr = copy.copy(ds.pixel_array)
                          # or even:
                          #        arr = copy.deepcopy(ds.pixel_array)
                          

                          before you proceed to qimage = QtGui.QImage(arr, ...). Any better, crash-wise?

                          D 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @darrenleeleelee1
                            I have no idea what is behind your

                                    ds = dcmread('./5F329172_20170623_CR_2_1_1')
                                    arr = ds.pixel_array
                            

                            It's some external library stuff. (And btw I have no idea whether arr.shape[1], arr.shape[0] is the right thing to use. Don't bother to explain, just make sure it's correct for whatever this ds stuff is.)

                            Try this:

                            import copy
                            ...
                                    arr = copy.copy(ds.pixel_array)
                            # or even:
                            #        arr = copy.deepcopy(ds.pixel_array)
                            

                            before you proceed to qimage = QtGui.QImage(arr, ...). Any better, crash-wise?

                            D Offline
                            D Offline
                            darrenleeleelee1
                            wrote on last edited by
                            #19

                            @JonB 72c6dea2-3187-4f9b-b755-d7939e474ce1-image.png
                            I add deepcopy,and it also crash. I add some arr attribute for you.
                            ed678234-91b9-475e-b889-0117c72ec04c-image.png

                            KroMignonK 1 Reply Last reply
                            0
                            • D darrenleeleelee1

                              @JonB 72c6dea2-3187-4f9b-b755-d7939e474ce1-image.png
                              I add deepcopy,and it also crash. I add some arr attribute for you.
                              ed678234-91b9-475e-b889-0117c72ec04c-image.png

                              KroMignonK Offline
                              KroMignonK Offline
                              KroMignon
                              wrote on last edited by KroMignon
                              #20

                              @darrenleeleelee1 I think you should change your code to:

                              qimage = QtGui.QImage(arr.data, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16)
                              

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              D 1 Reply Last reply
                              0
                              • KroMignonK KroMignon

                                @darrenleeleelee1 I think you should change your code to:

                                qimage = QtGui.QImage(arr.data, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16)
                                
                                D Offline
                                D Offline
                                darrenleeleelee1
                                wrote on last edited by
                                #21

                                @KroMignon I change but still crash...What is the different between arr and arr.data?

                                KroMignonK 1 Reply Last reply
                                0
                                • D darrenleeleelee1

                                  @KroMignon I change but still crash...What is the different between arr and arr.data?

                                  KroMignonK Offline
                                  KroMignonK Offline
                                  KroMignon
                                  wrote on last edited by
                                  #22

                                  @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                                  What is the different between arr and arr.data?

                                  arr is an instance of numpy.ndarray type, according to your code!
                                  And arr.data is the data buffer according to numpy documentation ==> https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html

                                  I think it would be great if you first explain what you want to do.
                                  Perhaps you should take a look at this python script which converts numpy.ndatarray to QImage and vice versa:
                                  https://kogs-www.informatik.uni-hamburg.de/~meine/software/vigraqt/qimage2ndarray.py

                                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                  D 1 Reply Last reply
                                  2
                                  • KroMignonK KroMignon

                                    @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                                    What is the different between arr and arr.data?

                                    arr is an instance of numpy.ndarray type, according to your code!
                                    And arr.data is the data buffer according to numpy documentation ==> https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html

                                    I think it would be great if you first explain what you want to do.
                                    Perhaps you should take a look at this python script which converts numpy.ndatarray to QImage and vice versa:
                                    https://kogs-www.informatik.uni-hamburg.de/~meine/software/vigraqt/qimage2ndarray.py

                                    D Offline
                                    D Offline
                                    darrenleeleelee1
                                    wrote on last edited by
                                    #23

                                    @KroMignon I saw this before, but I don't know how to apply to uint16.

                                    KroMignonK 1 Reply Last reply
                                    0
                                    • D darrenleeleelee1

                                      @KroMignon I saw this before, but I don't know how to apply to uint16.

                                      KroMignonK Offline
                                      KroMignonK Offline
                                      KroMignon
                                      wrote on last edited by
                                      #24

                                      @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                                      I saw this before, but I don't know how to apply to uint16.

                                      Which version of Qt-Libs are you using?
                                      Format_Grayscale16 is only support starting from Qt 5.13 (cf. QImage.Format)

                                      Because of this note from documentation:

                                      Constructs an image with the given width , height and format , that uses an existing memory buffer, data . The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

                                      Perhaps you should specify the number of bytes per line, like this:

                                      qimage = QtGui.QImage(arr.data, arr.shape[1], arr.shape[0], arr.shape[1]*2, QtGui.QImage.Format_Grayscale16)
                                      

                                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                      D 1 Reply Last reply
                                      0
                                      • KroMignonK KroMignon

                                        @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                                        I saw this before, but I don't know how to apply to uint16.

                                        Which version of Qt-Libs are you using?
                                        Format_Grayscale16 is only support starting from Qt 5.13 (cf. QImage.Format)

                                        Because of this note from documentation:

                                        Constructs an image with the given width , height and format , that uses an existing memory buffer, data . The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

                                        Perhaps you should specify the number of bytes per line, like this:

                                        qimage = QtGui.QImage(arr.data, arr.shape[1], arr.shape[0], arr.shape[1]*2, QtGui.QImage.Format_Grayscale16)
                                        
                                        D Offline
                                        D Offline
                                        darrenleeleelee1
                                        wrote on last edited by darrenleeleelee1
                                        #25

                                        @KroMignon said in PyQt QImage cna't copy, crash without error code:

                                        arr.shape[1]*2

                                        sorry, I read through the QImage API, but I don;t get why I need to add this.
                                        After I add this it seems can work.

                                        KroMignonK 1 Reply Last reply
                                        0
                                        • D darrenleeleelee1

                                          @KroMignon said in PyQt QImage cna't copy, crash without error code:

                                          arr.shape[1]*2

                                          sorry, I read through the QImage API, but I don;t get why I need to add this.
                                          After I add this it seems can work.

                                          KroMignonK Offline
                                          KroMignonK Offline
                                          KroMignon
                                          wrote on last edited by KroMignon
                                          #26

                                          @darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:

                                          sorry, I read through the QImage API, but I don;t get why I need to add this.

                                          Then you don't read carefully ;)
                                          The point is when you only specify width and height: The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

                                          To avoid this, you can use width, height and scanline length in bytes, which is pixel_per_line x pixel_size:

                                          • pixel_per_line is arr.shape[1]
                                          • pixel_size is uint16 which are 2 bytes

                                          ==> scanline_size = arr.shape[1]*2

                                          After I add this it seems can work.

                                          Great

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          D 1 Reply Last reply
                                          4

                                          • Login

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