QImage to Numpy Array using PySide

前端 未结 3 1180
时光说笑
时光说笑 2020-12-30 11:26

I am currently switching from PyQt to PySide.

With PyQt I converted QImage to a Numpy.Array using this code that I found on SO:

<         


        
3条回答
  •  鱼传尺愫
    2020-12-30 11:42

    For me the solution with constBits() did not work, but the following worked:

    def QImageToCvMat(incomingImage):
        '''  Converts a QImage into an opencv MAT format  '''
    
        incomingImage = incomingImage.convertToFormat(QtGui.QImage.Format.Format_RGBA8888)
    
        width = incomingImage.width()
        height = incomingImage.height()
    
        ptr = incomingImage.bits()
        ptr.setsize(height * width * 4)
        arr = np.frombuffer(ptr, np.uint8).reshape((height, width, 4))
        return arr
    

提交回复
热议问题