-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
make-screenshot.py
executable file
·62 lines (47 loc) · 1.48 KB
/
make-screenshot.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import contextlib
import os.path
import time
from selenium.webdriver.firefox.webdriver import WebDriver
SIZES = {
'1080p': (1920, 1080),
'720p': (1280, 720),
'panel': (320, 100),
}
@contextlib.contextmanager
def webdriver(*args, **kwargs):
driver = WebDriver(*args, **kwargs)
try:
yield driver
finally:
driver.quit()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('html_file')
parser.add_argument('--size', choices=SIZES, default='720p')
args = parser.parse_args()
# Put geckodriver on the PATH
bindir = os.path.abspath('bin')
path = os.environ['PATH']
os.environ['PATH'] = f'{bindir}{os.pathsep}{path}'
width, height = SIZES[args.size]
filepath = os.path.abspath(args.html_file)
basename, _ = os.path.splitext(args.html_file)
screenshot = f'{basename}-{args.size}.png'
with webdriver() as driver:
driver.get(f'file://{filepath}')
driver.execute_script(
f'window.open('
f' "file://{filepath}", "test",'
f' "innerWidth={width},innerHeight={height}"'
f');\n',
)
# Switch to our new window
handle, = set(driver.window_handles) - {driver.current_window_handle}
driver.switch_to.window(handle)
time.sleep(2)
driver.save_screenshot(screenshot)
if __name__ == '__main__':
raise SystemExit(main())