-
Notifications
You must be signed in to change notification settings - Fork 2
/
imagegrab.py
46 lines (35 loc) · 1.07 KB
/
imagegrab.py
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
"""Author FredHappyface.
Uses playwright to leverage a headless version of Chromium
"""
from __future__ import annotations
import os
from install_playwright import install
from PIL import Image
from playwright.sync_api import sync_playwright
def grabWebpage(url: str, resolution: tuple[int, int] = (800, 600), evalJs=None):
"""Take a screenshot of a webpage.
Args:
----
url (str): The url of the webpage in question
resolution ((int,int)), optional): Set the page resolution
evalJs (string): Javascript to run on the page
Returns:
-------
PIL.Image.Image: A PIL Image
"""
with sync_playwright() as p:
install(p.chromium)
browser = p.chromium.launch()
page = browser.new_page()
page.set_viewport_size({"width": resolution[0], "height": resolution[1]})
page.goto(url)
if evalJs is not None:
page.evaluate(evalJs)
page.screenshot(path="temp.png")
browser.close()
image = Image.open("temp.png")
try:
os.remove("temp.png")
except PermissionError:
print("WARNING: Unable to clean up, manually remove temp.png from project root or ignore")
return image