Mika Tähtinen


Tuesday 1.12.2009 14.44

Capturing webpage thumbnails using Python PyQt WebKit

Premium users in IRC-Galleria are allowed to customize their profile pages using CSS based modifications (mods). The picture above shows the tool that the users use to select from existing mods. Thumbnails for the mods are being generated using a small Python script that utilizes PyQt WebKit library and is controlled with Thrift.

Here’s a simplified pseudocode example of the process used to capture the web pages containing mods.

# The QWebPage used to render the pages needs a QApplication environment, so create it
app = QApplication([])

# app.exec_() blocks so use QTimer to shoot the actual main
QTimer().singleShot(0, main)
app.exec_()

...

def main():
    # wait for thrift calls and send them to snapshot()

...

def snapshot(url, filename):
    # Create page
    page = QWebPage()

    # Disable scrollbars from the captures
    frame.setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
    frame.setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)

    loading = True
    success = False

    # loadFinished event listener for updating loading and success
    def load_finished(result):
        loading = False
        success = result

    # Bind loadFinished event
    connect(page, SIGNAL("loadFinished(bool)", load_finished)

    # Start loading
    page.mainFrame().load(QUrl(url))

    # Wait
    while self.loading:
        QCoreApplication.processEvents()

    # Check result
    if success == False:
        return

    # Resize the browser
    size = page.viewportSize()
    size.setWidth(1024)
    size.setHeight(1024)
    page.setViewportSize(size)

    # Render the page to an QImage using QPainter
    image = QImage(size, QImage.Format_RGB32)
    painter = QPainter(image)
    page.mainFrame().render(painter)
    painter.end()

    # Scale and save to file
    image.scaled(200, 200, Qt.IgnoreAspectRatio, Qt.SmoothTransformation).save(filename, 'JPG', 95)
irc-galleriaduunigaltsudevpython