Aug-15-2020, 09:44 PM
I just need to take screenshot of a webpage and store / retrieve that image in mongodb. Below code I'm using to take screenshot of webpage and How I store it in a mongodb collection?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from selenium import webdriver from PIL import Image from io import BytesIO fox = webdriver.Firefox() # now that we have the preliminary stuff out of the way time to get that image :D element = fox.find_element_by_id( 'hlogo' ) # find part of the page you want image of location = element.location size = element.size png = fox.get_screenshot_as_png() # saves screenshot of entire page fox.quit() im = Image. open (BytesIO(png)) # uses PIL library to open image in memory left = location[ 'x' ] top = location[ 'y' ] right = location[ 'x' ] + size[ 'width' ] bottom = location[ 'y' ] + size[ 'height' ] im = im.crop((left, top, right, bottom)) # defines crop points im.save( 'screenshot.png' ) # saves new cropped image |