someuser2491
someuser2491

Reputation: 1968

How to capture screenshot in selenium with specific resolution?

I want to capture a screenshot of the canvas element with specific resolution. I am using below snippet to capture the screenshot. this works but always takes a screenshot of resolution 1544px*638px. based on what (browser window or my window screen)is it taking a screenshot. I am using chrome browser. how can I modify my code below to take a screenshot of the resolution say 500 * 325 or so.

I have used set_window_size(500, 325) and I get screenshot of size 750 * 135

def capture_screenshot():
    driver = LiveLibrary.get_webdriver_instance()
    driver.set_window_size(500, 325) 
    canvas_element = driver.find_element_by_xpath("//canvas")
    result = canvas_element.screenshot_as_png
    with open('save.png', 'wb') as f:
        f.write(result)

could someone help me with this. thanks.

Upvotes: 1

Views: 3451

Answers (1)

Renat
Renat

Reputation: 8962

You may resize the screenshot with Image.resize from PIL. Like:

from PIL import Image
import io

    ...

    result = canvas_element.screenshot_as_png
    image = Image.open(io.BytesIO(result))
    imageResized = image.resize( (500,325), Image.ANTIALIAS) 
    with open('save.png', 'wb') as f:
        imageResized.save(f , format='PNG') 

Upvotes: 2

Related Questions