将PyQt5 QPixmap转换为numpy ndarray [英] Convert PyQt5 QPixmap to numpy ndarray

查看:1239
本文介绍了将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

I want to convert it to OpenCV format. I tried to convert it to numpy.ndarray as described here but I got error sip.voidptr object has an unknown size

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

Is there any way to get numpy array (same format as cv2.VideoCapture read method returns)?

推荐答案

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

I got numpy array using this code:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆