将numpy数组转换为PySide QPixmap
我想将图像转换为NumPy数组再转换为PySide QPixmap,因此可以显示它(在PySide
UI中编辑:)。我已经找到了此工具:qimage2ndarray,但它仅适用于PyQt4。我试图对其进行更改以使其能够与PySide一起使用,但是我将不得不更改该工具的C部分,并且我没有使用C的经验。我该怎么做?或者有其他选择吗?
-
一种选择是仅使用PIL库。
>>> import numpy as np >>> import Image >>> im = Image.fromarray(np.random.randint(0,256,size=(100,100,3)).astype(np.uint8)) >>> im.show()
您可以在http://www.pyside.org/docs/pyside/PySide/QtGui/QImage.html上查看QPixmap构造函数。
看起来您应该能够直接在构造函数中使用numpy数组:
PySide.QtGui.QImage类(数据,宽度,高度,格式)
其中format参数是以下参数之一:http
:
//www.pyside.org/docs/pyside/PySide/QtGui/QImage.html#PySide.QtGui.PySide.QtGui.QImage.Format。因此,例如,您可以执行以下操作:
>>> a = np.random.randint(0,256,size=(100,100,3)).astype(np.uint32) >>> b = (255 << 24 | a[:,:,0] << 16 | a[:,:,1] << 8 | a[:,:,2]).flatten() # pack RGB values >>> im = PySide.QtGui.QImage(b, 100, 100, PySide.QtGui.QImage.Format_RGB32)
我没有安装PySide,所以我没有对此进行测试。它可能无法按原样工作,但可能会引导您朝正确的方向发展。