File size: 1,582 Bytes
806821a
8a6fe3b
 
 
 
fcfab3e
8a6fe3b
 
 
fcfab3e
 
 
bf53a82
fcfab3e
bf53a82
8a6fe3b
925a534
8a6fe3b
be92e06
8a6fe3b
fcfab3e
 
 
 
 
1c94dc0
 
2bf0206
 
 
 
 
 
 
175a26c
2bf0206
7fe2c0f
 
fcfab3e
8a6fe3b
fcfab3e
 
8a6fe3b
 
 
9b78455
8a6fe3b
 
fcfab3e
1c94dc0
8a6fe3b
1c94dc0
8a6fe3b
806821a
a92ebbf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from PIL import Image
from io import BytesIO
import time

def take_screenshot(url):
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")

    wd = None
    try:
        wd = webdriver.Chrome(options=options)
        wd.set_window_size(1280, 1280)  # Set window size to 1920x1080
        wd.get(url)
        time.sleep(5)
        screenshot = wd.get_screenshot_as_png()

        if len(screenshot) < 1000:
            print("⚠ Screenshot too small, likely error.")
            return None

        image = Image.open(BytesIO(screenshot))

        # Center crop 
        width, height = image.size
        crop_size_side = 1280
        crop_size_top = 1280
        left = (width - crop_size_side) // 2
        top = (height - crop_size_top) // 2
        right = left + 1000
        bottom = top - 500
        cropped_image = image.crop((left, top, right, bottom))

        return image

    except WebDriverException as e:
        print("WebDriverException:", e)
        return None
    finally:
        if wd:
            wd.quit()

iface = gr.Interface(
    fn=take_screenshot,
    inputs=gr.Textbox(label="Website URL", value="https://github.com"),
    outputs=gr.Image(type="pil", label="Cropped Screenshot"),
    title="Website Screenshot",
    description="Takes a 1920x1080 screenshot of the website, center-cropped to 900x900."
)

iface.launch()