img = fangfalei.screen.grabWindow(hwnd).toImage() 如何用cv2 打开
时间: 2024-03-21 15:44:17 浏览: 14
可以使用PIL库将Qt的QImage转换为numpy数组,然后再使用cv2打开。具体实现代码如下:
```python
import cv2
from PIL import Image
# 假设img为Qt的QImage对象
img = fangfalei.screen.grabWindow(hwnd).toImage()
# 将QImage转换为numpy数组
buffer = img.bits().asstring(img.width() * img.height() * 4)
image = np.frombuffer(buffer, dtype=np.uint8).reshape((img.height(), img.width(), 4))
image = image[:, :, :3] # 去除alpha通道
# 将numpy数组转换为PIL Image对象
pil_image = Image.fromarray(image)
# 使用cv2打开PIL Image对象
cv2_image = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2BGR)
cv2.imshow('image', cv2_image)
cv2.waitKey(0)
```