Created
November 11, 2016 13:55
-
-
Save Sebastian1011/aeadd6c598826f0f8e42efaf62f8cf50 to your computer and use it in GitHub Desktop.
python screenshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import time | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
from PyQt4.QtWebKit import * | |
class Screenshot(QWebView): | |
def __init__(self): | |
self.app = QApplication(sys.argv) | |
QWebView.__init__(self) | |
self._loaded = False | |
self.loadFinished.connect(self._loadFinished) | |
def capture(self, url, output_file): | |
self.load(QUrl(url)) | |
self.wait_load() | |
# set to webpage size | |
frame = self.page().mainFrame() | |
self.page().setViewportSize(frame.contentsSize()) | |
# render image | |
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32) | |
painter = QPainter(image) | |
frame.render(painter) | |
painter.end() | |
print 'saving', output_file | |
image.save(output_file) | |
def wait_load(self, delay=0): | |
# process app events until page loaded | |
while not self._loaded: | |
self.app.processEvents() | |
time.sleep(delay) | |
self._loaded = False | |
def _loadFinished(self, result): | |
self._loaded = True | |
# s = Screenshot() | |
# s.capture('http://yt-dragonfly.tech/dragonfly', 'website.png') | |
# s.capture('http://baidu.com', 'blog.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment