2

I am using skimage to do some image manipulations via their numpy manipulations. I am able to do the math on my pixels and then show the result using

def image_manip():
    # do manipulations
    return final_image
viewer = ImageViewer(image_manip())
viewer.show()

In parallel, in a different application, I'm able to show an image in QT using:

self.pixmap = QtGui.QPixmap('ImagePath.jpg')

So ideally, I'd like to combine the two into something like this:

def image_manip():
    # do manipulations
    return final_image

self.pixmap = QtGui.QPixmap(image_manip())

Obviously this doesn't work. I get an error TypeError: QPixmap(): argument 1 has unexpected type 'numpy.ndarray'

My guess is that viewer = ImageViewer(image_manip()) and viewer.show() has some magic to allow it to read the skimage/numpy objects directly. In my use case, I don't want to save a file out of skimage (I want to just keep it in memory), so I would imagine it needs to be 'baked out' so Qt can read it as a common format.

How do I go about doing this?

M Leonard
  • 581
  • 1
  • 7
  • 23
  • 2
    Possible duplicate of [Convert Python Opencv Image (numpy array) to PyQt QPixmap image](https://stackoverflow.com/questions/34232632/convert-python-opencv-image-numpy-array-to-pyqt-qpixmap-image) – Imanol Luengo Oct 15 '17 at 19:01

1 Answers1

2

You can convert a uint8 numpy array (shape M, N, 3 RGB image) to QPixmap as follows:

from skimage import img_as_ubyte

arr = img_as_ubyte(arr)
img = QImage(arr.data, arr.shape[1], arr.shape[0],
             arr.strides[0], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(img)
Stefan van der Walt
  • 7,165
  • 1
  • 32
  • 41
  • I ended up implementing it a little different way, but yes! This works! And what differentiated your answer from the proposed duplicate answer was `img_as_ubyte` ... otherwise, the image was just a bunch of colorful pixels :-) Thanks! – M Leonard Oct 15 '17 at 19:12