-
Notifications
You must be signed in to change notification settings - Fork 2
/
web2png.py
49 lines (36 loc) · 1.56 KB
/
web2png.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
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QUrl, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
class QWebScreenshot(QWebEngineView):
def capture(self, url, output_file):
self.output_file = output_file
self.load(QUrl(url))
self.loadFinished.connect(self.on_loaded)
# Create hidden view without scrollbars
self.setAttribute(Qt.WA_DontShowOnScreen)
self.page().settings().setAttribute(QWebEngineSettings.ShowScrollBars, False)
self.show()
def on_loaded(self):
size = self.page().contentsSize().toSize()
self.resize(size)
# Wait for resize
QTimer.singleShot(1000, self.take_screenshot)
def take_screenshot(self):
self.grab().save(self.output_file, b'PNG')
self.app.exit(0)
app = QApplication(sys.argv)
def take_chess_screenshot(fen_string=None, output_filename=None):
# Take uncropped screenshot of lichess board of FEN string and save to file
url_template = "http://en.lichess.org/editor/%s"
s = QWebScreenshot()
s.app = app
s.capture(url_template % fen_string, output_filename)
return app.exec_()
def take_screenshot(url=None, output_filename=None):
s = QWebScreenshot()
s.app = app
s.capture(url, output_filename)
return app.exec_()
# take_chess_screenshot(fen_string='rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR',output_filename='webpage.png')
# take_chess_screenshot(fen_string='rnbqkbnr/pppppppp/7Q/8/4P3/8/PPPP1PPP/RNBQKBNR',output_filename='webpage2.png')