将PyQt5 QPixmap转换为numpy ndarray

2024-09-27 09:37:51 发布

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

我有pixmap:

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

我想把它转换成OpenCV格式。 我试图将其转换为numpy.ndarray,如here所述,但我得到了错误sip.voidptr object has an unknown size

有没有办法获得numpy数组(与cv2.VideoCaptureread方法返回的格式相同)?在


Tags: selfnumpyhereobject格式错误widthscreen
2条回答

我用这个代码得到了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)) 

可以通过执行以下操作来避免复制:

channels_count = 4
pixmap = self._screen.grabWindow(0, self._x, self._y, self._width, self._height)
image = pixmap.toImage()
b = image.bits()
# sip.voidptr must know size to support python buffer interface
b.setsize(self._height * self._width * channels_count)
arr = np.frombuffer(b, np.uint8).reshape((self._height, self._width, channels_count))

相关问题 更多 >

    热门问题