简体   繁体   中英

Show skimage output in qt application

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM