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.
  • K KroMignon
    7 Jun 2021, 13:25

    @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 7 Jun 2021, 13:36 last edited by
    #21

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

    K 1 Reply Last reply 7 Jun 2021, 13:51
    0
    • D darrenleeleelee1
      7 Jun 2021, 13:36

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

      K Offline
      K Offline
      KroMignon
      wrote on 7 Jun 2021, 13:51 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 8 Jun 2021, 01:43
      2
      • K KroMignon
        7 Jun 2021, 13:51

        @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 8 Jun 2021, 01:43 last edited by
        #23

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

        K 1 Reply Last reply 8 Jun 2021, 05:45
        0
        • D darrenleeleelee1
          8 Jun 2021, 01:43

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

          K Offline
          K Offline
          KroMignon
          wrote on 8 Jun 2021, 05:45 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 8 Jun 2021, 07:09
          0
          • K KroMignon
            8 Jun 2021, 05:45

            @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 8 Jun 2021, 07:09 last edited by darrenleeleelee1 6 Aug 2021, 07:09
            #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.

            K 1 Reply Last reply 8 Jun 2021, 07:15
            0
            • D darrenleeleelee1
              8 Jun 2021, 07:09

              @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.

              K Offline
              K Offline
              KroMignon
              wrote on 8 Jun 2021, 07:15 last edited by KroMignon 6 Aug 2021, 07:16
              #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 8 Jun 2021, 11:51
              4
              • K KroMignon
                8 Jun 2021, 07:15

                @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

                D Offline
                D Offline
                darrenleeleelee1
                wrote on 8 Jun 2021, 11:51 last edited by
                #27

                @KroMignon @J-Hilk @JonB @jsulm thanks all u guys!

                1 Reply Last reply
                1

                21/27

                7 Jun 2021, 13:36

                • Login

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