1#!/usr/bin/env python 2""" 3Capture a web page and save its internal frames in different images 4 5 framecapture.py <url> <outputfile> 6 7Notes: 8 'url' is the URL of the web page to be captured 9 'outputfile' is the prefix of the image files to be generated 10 11Example: 12 framecapture qt.nokia.com trolltech.png 13 14Result: 15 trolltech.png (full page) 16 trolltech_frame1.png (...) trolltech_frameN.png ('N' number of internal frames) 17""" 18 19 20############################################################################# 21## 22## Copyright (C) 2013 Riverbank Computing Limited 23## Copyright (C) 2010 Hans-Peter Jansen <hpj@urpla.net>. 24## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 25## All rights reserved. 26## 27## This file is part of the examples of PyQt. 28## 29## $QT_BEGIN_LICENSE:BSD$ 30## You may use this file under the terms of the BSD license as follows: 31## 32## "Redistribution and use in source and binary forms, with or without 33## modification, are permitted provided that the following conditions are 34## met: 35## * Redistributions of source code must retain the above copyright 36## notice, this list of conditions and the following disclaimer. 37## * Redistributions in binary form must reproduce the above copyright 38## notice, this list of conditions and the following disclaimer in 39## the documentation and/or other materials provided with the 40## distribution. 41## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 42## the names of its contributors may be used to endorse or promote 43## products derived from this software without specific prior written 44## permission. 45## 46## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 47## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 48## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 49## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 50## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 51## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 52## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 56## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 57## $QT_END_LICENSE$ 58## 59########################################################################### 60 61import sys 62 63from PyQt5.QtCore import pyqtSignal, QObject, QSize, Qt, QUrl 64from PyQt5.QtGui import QImage, QPainter 65from PyQt5.QtWidgets import QApplication 66from PyQt5.QtWebKitWidgets import QWebPage 67 68 69def cout(s): 70 sys.stdout.write(s) 71 sys.stdout.flush() 72 73 74def cerr(s): 75 sys.stderr.write(s) 76 sys.stderr.flush() 77 78 79class FrameCapture(QObject): 80 81 finished = pyqtSignal() 82 83 def __init__(self): 84 super(FrameCapture, self).__init__() 85 86 self._percent = 0 87 self._page = QWebPage() 88 self._page.mainFrame().setScrollBarPolicy(Qt.Vertical, 89 Qt.ScrollBarAlwaysOff) 90 self._page.mainFrame().setScrollBarPolicy(Qt.Horizontal, 91 Qt.ScrollBarAlwaysOff) 92 self._page.loadProgress.connect(self.printProgress) 93 self._page.loadFinished.connect(self.saveResult) 94 95 def load(self, url, outputFileName): 96 cout("Loading %s\n" % url.toString()) 97 self._percent = 0 98 index = outputFileName.rfind('.') 99 self._fileName = index == -1 and outputFileName + ".png" or outputFileName 100 self._page.mainFrame().load(url) 101 self._page.setViewportSize(QSize(1024, 768)) 102 103 def printProgress(self, percent): 104 if self._percent >= percent: 105 return 106 self._percent += 1 107 while self._percent < percent: 108 self._percent += 1 109 cout("#") 110 111 def saveResult(self, ok): 112 cout("\n") 113 # Crude error-checking. 114 if not ok: 115 cerr("Failed loading %s\n" % self._page.mainFrame().url().toString()) 116 self.finished.emit() 117 return 118 119 # Save each frame in different image files. 120 self._frameCounter = 0 121 self.saveFrame(self._page.mainFrame()) 122 self.finished.emit() 123 124 def saveFrame(self, frame): 125 fileName = self._fileName 126 if self._frameCounter: 127 index = fileName.rfind('.') 128 fileName = "%s_frame%s%s" % (fileName[:index], self._frameCounter, fileName[index:]) 129 image = QImage(frame.contentsSize(), QImage.Format_ARGB32_Premultiplied) 130 image.fill(Qt.transparent) 131 painter = QPainter(image) 132 painter.setRenderHint(QPainter.Antialiasing, True) 133 painter.setRenderHint(QPainter.TextAntialiasing, True) 134 painter.setRenderHint(QPainter.SmoothPixmapTransform, True) 135 frame.documentElement().render(painter) 136 painter.end() 137 image.save(fileName) 138 self._frameCounter += 1 139 for childFrame in frame.childFrames(): 140 self.saveFrame(childFrame) 141 142 143if __name__ == '__main__': 144 if len(sys.argv) != 3: 145 cerr(__doc__) 146 sys.exit(1) 147 148 url = QUrl.fromUserInput(sys.argv[1]) 149 fileName = sys.argv[2] 150 151 app = QApplication(sys.argv) 152 153 capture = FrameCapture() 154 capture.finished.connect(app.quit) 155 capture.load(url, fileName) 156 157 app.exec_() 158