使用PySid从QImage到Numpy数组

2024-09-20 04:00:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从PyQt切换到PySide。

使用PyQt,我使用在SO上找到的代码将QImage转换为Numpy.Array

def convertQImageToMat(incomingImage):
    '''  Converts a QImage into an opencv MAT format  '''

    incomingImage = incomingImage.convertToFormat(4)

    width = incomingImage.width()
    height = incomingImage.height()

    ptr = incomingImage.bits()
    ptr.setsize(incomingImage.byteCount())
    arr = np.array(ptr).reshape(height, width, 4)  #  Copies the data
    return arr

但是ptr.setsize(incomingImage.byteCount())不适用于PySide,因为这是PyQt的^{} support的一部分。

我的问题是:如何使用PySide将QImage转换为Numpy.Array

编辑:

Version Info
> Windows 7 (64Bit)
> Python 2.7
> PySide Version 1.2.1
> Qt Version 4.8.5

Tags: 代码numpysoversionwidtharraypyqtpyside