如何以高分辨率捕获网站截图? [英] How to capture website screenshot in high resolution?

查看:517
本文介绍了如何以高分辨率捕获网站截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to capture screenshot of a website in high resolution to recognize text or simply to save high quality images. I tried this code in Python 2.7. The website http://www.flaticon.com/ has been taken merely as example.

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()

它捕获了屏幕截图,但是分辨率对我来说还不够.扩大窗口大小并不能解决问题.来自Webside的图片仅驻留在图像的一部分上.似乎图像分辨率没有受到影响. 有什么方法可以在保存之前显式设置图像分辨率?

It captures screenshot, but the resolution is not enough for me. Enlarging window size doesn`t resolve the problem. The picture from webside reside only on the part of the image. It seems that image resolution is not affected. Is there some way to explicitly set image resolution before saving it?

推荐答案

如果要更改窗口大小,可以通过以下方式进行设置

If it is about changing window size, you can set it by

driver.set_window_size(480, 320)

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

Here is an example of doing this from Github of one of the developers. As you can see, you can adjust both Window size and quality of the screenshot.

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-最小.

The quality of 100 is max, 0 - min.

您也可以使用selenium.windowMaxmize().

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

And if you want to magnify the screen to see some specific texts as you said, you can try this in Mozilla:

from selenium.webdriver.common.keys import Keys    
from selenium.webdriver.common.action_chains import ActionChains

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()

这篇关于如何以高分辨率捕获网站截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆