QImage to Numpy Array using PySide

I am currently switching from PyQt to PySide.

With PyQt, I converted QImage to Numpy.Array using this code, which I found in https://stackoverflow.com/a/3/2/2/ ::

 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 

However, ptr.setsize(incomingImage.byteCount()) does not work with PySide, as it is part of void* PyQt support .

My question is: how to convert QImage to Numpy.Array using PySide.

EDIT:

 Version Info > Windows 7 (64Bit) > Python 2.7 > PySide Version 1.2.1 > Qt Version 4.8.5 
+6
source share
2 answers

PySide does not offer the bits method. How about using constBits to get a pointer to an array?

+2
source

The trick is to use QImage.constBits() , as suggested by @Henry Gomersall. The code I'm using now:

 def QImageToCvMat(self,incomingImage): ''' Converts a QImage into an opencv MAT format ''' incomingImage = incomingImage.convertToFormat(QtGui.QImage.Format.Format_RGB32) width = incomingImage.width() height = incomingImage.height() ptr = incomingImage.constBits() arr = np.array(ptr).reshape(height, width, 4) # Copies the data return arr 
+1
source

Source: https://habr.com/ru/post/957816/


All Articles