Convert numpy array to PySide QPixmap

前端 未结 4 901
借酒劲吻你
借酒劲吻你 2020-12-08 16:59

I want to convert an image into a NumPy array to a PySide QPixmap, so I can display it (EDIT: in my PySide UI). I already found this tool: qimage2ndarray, but it only works

4条回答
  •  臣服心动
    2020-12-08 17:17

    In addition to @user545424 answer about using PIL, if you didn't want to depend on PIL, you could manually construct your Image directly from your np array:

    width = 100
    height = 100
    data = np.random.randint(0,256,size=(width,height,3)).astype(np.uint8)
    
    img = QtGui.QImage(width, height, QtGui.QImage.Format_RGB32)
    for x in xrange(width):
        for y in xrange(height):
            img.setPixel(x, y, QtGui.QColor(*data[x][y]).rgb())
    
    pix = QtGui.QPixmap.fromImage(img)
    

    I'm sure, using PIL, there is a way to read the actual image data into a QImage, but I will let @user545424 address that part since its from his answer. PIL comes with the ImageQt module which is convenient for directly converting an Image -> QPixmap, but unfortunately thats a PyQt4 QPixmap, which doesn't help you.

提交回复
热议问题