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. Numpy grayscale array (2D) to RGB QImage or QPixmap in either red, green or blue
Forum Updated to NodeBB v4.3 + New Features

Numpy grayscale array (2D) to RGB QImage or QPixmap in either red, green or blue

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2qt for python
2 Posts 1 Posters 3.3k 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 last edited by
    #1

    Hi,
    I need to convert a numpy array to a QImage (or QPixmap), I tried passing my array as the argument to QImage constructor and I also tried the .tobytes but the produced image doesn't seem correct.
    I am having a hard time with this and been working on it for over a day, some help would be very appreciated.
    I am using PySide2 on OS X.

    Thank you!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Roulbac
      wrote on last edited by
      #2

      So I solved my problem.
      It turns out QImage needs the image to be a flat numpy array.
      Here is how it's done:

              qrgb_dict = {'r': lambda x: QtGui.qRgb(x, 0, 0),
                           'g': lambda x: QtGui.qRgb(0, x, 0),
                           'b': lambda x: QtGui.qRgb(0, 0, x)}
              colortable = [qrgb_dict[color](i) for i in range(256)]
              img = QtGui.QImage(arr, w, h, w, QtGui.QImage.Format_Indexed8)
              img.setColorTable(colortable)
      

      Where arr is an array of shape (h,w) and with uint8 values in 0-255

      1 Reply Last reply
      1

      • Login

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