截屏转成灰度图
# 通过base64
from selenium import webdriver import cv2 import base64 import numpy as np driver = webdriver.Chrome() driver.get('http://www.baidu.com') index64 = driver.get_screenshot_as_base64() indexstring = base64.b64decode(index64) nparr = np.frombuffer(indexstring, np.uint8) image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('gray', image_gray) cv2.waitKey(0)
#通过png, pillow,
from selenium import webdriver import numpy as np from PIL import Image from io import BytesIO import cv2 driver = webdriver.Chrome() index = driver.get('http://www.baidu.com') scr = driver.get_screenshot_as_png() scr = Image.open(BytesIO(scr)) scr = np.asarray(scr, dtype=np.float32).astype(np.uint8) scr = cv2.cvtColor(scr, cv2.COLOR_BGR2GRAY) cv2.imshow('gray', scr) cv2.waitKey(0)