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. QImage data array row-major or column-major order?

QImage data array row-major or column-major order?

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2qt for python
3 Posts 3 Posters 963 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.
  • R Offline
    R Offline
    Roulbac
    wrote on 20 May 2019, 19:45 last edited by
    #1

    Hi all,

    I am developing an application and It looks like I had a bug that comes from the fact that making a QImage off of a numpy array of shape (h,w) only works if the array is flattened in column-major order before being passed to QImage constructor.

    h, w = arr.shape[0], arr.shape[1]
    arr = (255*arr).astype(np.uint8).flatten(order='F')
    img = QtGui.QImage(
                arr,
                w, h,
                h*np.nbytes[np.uint8],
                QtGui.QImage.Format_Indexed8)
    

    I know the QPainter origin is the top left corner, however does bytePerLine mean the number of bytes per row or per column of my initial array?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 23 May 2019, 17:46 last edited by
      #2

      where do you use bytePerLine in the code? I don't see it.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Denni
        wrote on 5 Jun 2019, 14:13 last edited by Denni 6 May 2019, 14:14
        #3

        It appears they are using the following version of the overloaded QImage call

        QtGui.QImage(uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)

        Thus the h*np.nbytes[np.uint8] would be the bytesPerLine

        That being said here is an augmented bit of documentation (https://doc.qt.io/qt-5/qimage.html#QImage-5) covering this function that I think will answer the basic question, cannot guarantee it will not raise additional questions though

        This 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. The bytesPerLine specifies the number of bytes per line (stride). For information on Stride see: (https://medium.com/@oleg.shipitko/what-does-stride-mean-in-image-processing-bba158a72bcd)

        The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

        If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.

        madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

        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