Forum Archive

How to render a web background and save the whole page image to local?

louxingliang

I want to write a python script to replace me screenshot a web. I just need to input the website such as "www.google.com", then the script will save the whole page to local. How to realize it ?

I made a simple test script, but it seems don't work...

from ui import WebView, ImageContext

w=WebView()
w.load_url('http://www.baidu.com')

with ImageContext(w.width,w.hight) as ctx:
    w.draw_snapshot()
cvp

@louxingliang try this. Snap button enable when web page loaded. Image saved as snap.jpg in same folder as your script

import ui
import io
from PIL import Image

v = ui.WebView()
b = ui.ButtonItem()
class MyWebViewDelegate ():
    def webview_did_finish_load(self, webview):
        b.enabled = True
v.delegate = MyWebViewDelegate()
def snap(sender):
    # create image with infos TextView 
    with ui.ImageContext(v.width, v.height) as ctx:
        v.draw_snapshot()
        pil = Image.open(io.BytesIO(ctx.get_image().to_png())) # ui.image -> pil
    pil.save('snap.jpg', quality=95)
b.title = 'snap'
b.enabled = False
b.action = snap
v.right_button_items = (b,)
v.present()
v.load_url('http://www.baidu.com')
louxingliang

Thanks! This script works quite well!