如何以高分辨率捕获网站截图?

时间:2016-09-20 17:23:23

标签: python selenium-webdriver

我希望以高分辨率捕获网站的屏幕截图以识别文本或仅保存高质量图像。我在Python 2.7中尝试了这段代码。网站http://www.flaticon.com/仅作为示例。

from selenium import webdriver
import time
driver = webdriver.PhantomJS()
#Setting large window size doesn`t resolve the problem
driver.set_window_size(16000, 12000)
driver.get('http://www.flaticon.com/')
time.sleep(3)
#set resolution 640 dots per inch for this image 
#???
driver.save_screenshot('./downloaded/img/welcome_icons.png') # save a screenshot to disk
driver.close()

它捕获截图,但分辨率对我来说还不够。放大窗口大小并不能解决问题。来自网站的图片仅驻留在图像的一部分。似乎图像分辨率不受影响。 有没有办法在保存之前明确设置图像分辨率?

3 个答案:

答案 0 :(得分:4)

有点hacky,但我通过将窗口大小增加到3000并将缩放增加到250%来解决这个问题。

driver.set_window_size(3000,800)

driver.execute_script("document.body.style.zoom='250%'")

希望这有帮助。

答案 1 :(得分:3)

如果是关于更改窗口大小,可以通过

进行设置
driver.set_window_size(480, 320)

以下是其中一位开发人员的Github执行此操作的示例。如您所见,您可以调整窗口大小和屏幕截图的质量。

import StringIO
from selenium import webdriver
from PIL import Image


# Install instructions
#
# npm install phantomjs
# sudo apt-get install libjpeg-dev
# pip install selenium pillow


driver = webdriver.PhantomJS(executable_path="node_modules/phantomjs/bin/phantomjs")
driver.set_window_size(1366, 728) # optional
driver.get('http://google.com')
driver.save_screenshot('screen_hires.png')

screen = driver.get_screenshot_as_png()

# Crop it back to the window size (it may be taller)
box = (0, 0, 1366, 728)
im = Image.open(StringIO.StringIO(screen))
region = im.crop(box)
region.save('screen_lores.jpg', 'JPEG', optimize=True, quality=95)

100的质量是最大,0 - 分钟。

修改

您也可以使用selenium.windowMaxmize()

如果您想放大屏幕以查看您所说的某些特定文本,可以在Mozilla中尝试:

from selenium.webdriver.common.keys import Keys    

br = webdriver.Firefox()
zoom = ActionChains(br)
body = br.find_element_by_tag_name('body')
for i in range(2):
    zoom.send_keys_to_element(body,Keys.CONTROL,"+").perform()

答案 2 :(得分:3)

使用Python 3.7和chrome webdriver时,我遇到了完全相同的问题,上面的答案对我有很大帮助,但对我来说却没有完美的解决方案。这是我找到的解决方案:

from selenium import webdriver
from PIL import Image
from io import BytesIO
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# MUST BE HEADLESS AND HAVE VERY LARGE WINDOW SIZE
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=6000x5000")

def get_screenshot(url_path, object_type, object_id):
    chrome = webdriver.Chrome(chrome_options=chrome_options, 
executable_path='/path/to/webdriver')
    chrome.get('http://website.com' + url_path)
    chrome.execute_script("document.body.style.zoom = '300%'")     # ZOOM

    element = chrome.find_element_by_id(object_id) # find part of the page you want image of
    location = element.location
    size = element.size
    png = chrome.get_screenshot_as_png() # saves screenshot of entire page
    chrome.quit()

    im = Image.open(BytesIO(png)) # uses PIL library to open image in memory

    left = location['x'] * 3 # must mutliply all these numbers by your zoom
    top = location['y'] * 3
    right = (location['x'] + size['width']) * 3
    bottom = (location['y'] + size['height']) * 3

    im = im.crop((left, top, right, bottom)) # defines crop points
    im.save(f'{object_id}.png') # saves new cropped image