Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver, screenshot as numpy array (Python)

Is there a way to take a screenshot with selenium webdriver and convert in to a numpy array instead of saving it? I need to use it with openCV.

Note: I don't want to save the image and open it again

like image 696
abc Avatar asked Aug 30 '14 16:08

abc


1 Answers

I'm sure there is a more efficient way of doing this, but this is what worked for me:

from selenium import webdriver
from PIL import Image
import StringIO
import numpy as np

browser = webdriver.Firefox()
browser.get('https://www.google.ca/?gws_rd=ssl')

data = browser.get_screenshot_as_png()

img = Image.open(StringIO.StringIO(data))

numpy_array = np.asarray(img)
like image 51
abc Avatar answered Oct 28 '22 23:10

abc