Skip to content

Instantly share code, notes, and snippets.

@junmakii
Created October 14, 2014 14:41
Show Gist options
  • Select an option

  • Save junmakii/11f1394f05a09577775e to your computer and use it in GitHub Desktop.

Select an option

Save junmakii/11f1394f05a09577775e to your computer and use it in GitHub Desktop.
import sys, 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://www.google.com", "/tmp/google.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment