1

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.

4
  • The taken screenshot depends on your screen resolution... I believe you can't manipulate that. You can use OpenCV library to work-around this Commented Sep 27, 2019 at 10:01
  • by resolution do you mean size? or actual quality of image? Commented Sep 27, 2019 at 10:07
  • size (width and height) Commented Sep 27, 2019 at 10:07
  • have you tried using PIL to resize it ? Commented Sep 27, 2019 at 10:09

1 Answer 1

2

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') 
Sign up to request clarification or add additional context in comments.

4 Comments

i have to use this image for comparison as well later.
if comparison takes place in the same python script, you may return the image from capture_screenshot()
why would you change the quality of Image (instead of the default 80)?
@wishmaster, thanks for noticing that, removed it, of course quality should not be set as png is a lossless format, my bad, I copypasted the save call without much thinking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.