将PyQt5 QPixmap转换为numpy ndarray

我有像素图:

pixmap = self._screen.grabWindow(0,

self._x, self._y,

self._width, self._height)

我想将其转换为OpenCV格式。我尝试将其转换numpy.ndarray为此处所述,但出现错误sip.voidptr object has an unknown size

有什么方法可以获取numpy数组(与cv2.VideoCaptureread方法返回的格式相同)?

回答:

我使用以下代码获取了numpy数组:

channels_count = 4

pixmap = self._screen.grabWindow(0, self._x, self._y, self._width, self._height)

image = pixmap.toImage()

s = image.bits().asstring(self._width * self._height * channels_count)

arr = np.fromstring(s, dtype=np.uint8).reshape((self._height, self._width, channels_count))

以上是 将PyQt5 QPixmap转换为numpy ndarray 的全部内容, 来源链接: utcz.com/qa/397367.html

回到顶部