img = fangfalei.screen.grabWindow(hwnd).toImage() img使用CV2 打开
时间: 2024-03-21 07:44:37 浏览: 27
您可以使用OpenCV库的cv2.imdecode()函数打开img,示例代码如下:
```
import cv2
import numpy as np
from PyQt5.QtGui import QImage, QPixmap
# 将QImage转为numpy array
def qimage_to_array(qimage):
buffer = qimage.bits()
buffer.setsize(qimage.byteCount())
return np.array(buffer).reshape(qimage.height(), qimage.width(), 4)
# 将QPixmap转为numpy array
def qpixmap_to_array(qpixmap):
return qimage_to_array(qpixmap.toImage())
# 将Qt截图转为OpenCV格式
def qt_screen_to_opencv(qt_screen):
# 将QPixmap转为numpy array
np_image = qpixmap_to_array(qt_screen)
# 将RGBA格式转为BGR格式
bgr_image = cv2.cvtColor(np_image, cv2.COLOR_RGBA2BGR)
return bgr_image
# 加载Qt截图
qt_screen = QPixmap('screenshot.png')
# 将Qt截图转为OpenCV格式
opencv_image = qt_screen_to_opencv(qt_screen)
# 显示OpenCV图片
cv2.imshow('Image', opencv_image)
cv2.waitKey()
```
其中,`qpixmap_to_array()`和`qimage_to_array()`函数用于将Qt的QPixmap和QImage对象转为numpy数组,`qt_screen_to_opencv()`函数将Qt截图转为OpenCV格式的图像。您可以根据您的需要修改代码。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)