OpenCV没有截取屏幕的功能,而PyAutoGui有,它俩结合起来,OpenCV就可以使用PyAutoGui的截图了。
具体Python实现如下:
import pyautogui
import cv2
import numpy as np
img = pyautogui.screenshot()
open_cv_image = np.array(img)
# Convert RGB to BGR,opencv read image as BGR,but Pillow is RGB
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
cv2.imwrite('image.jpg', open_cv_image)
# cv2.imshow('image', open_cv_image)
需要注意的是这一行代码
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
因为OpenCV读取的图片顺序是BGR,而Pillow是RGB,PyAutoGui用的是Pillow,所以需要把RGB转换为BGR,不然OpenCV中的颜色会不对,发蓝。
PS:opencv先通过pip3 install opencv-python
安装。