Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created February 19, 2011 09:53
Show Gist options
  • Save ynkdir/834971 to your computer and use it in GitHub Desktop.
Save ynkdir/834971 to your computer and use it in GitHub Desktop.
webthumbnail.py
# Webpage thumbnailer
# encoding: utf-8
from __future__ import print_function, division, unicode_literals, \
absolute_import
import sys
import argparse
from PySide.QtCore import Signal, Qt, QObject
from PySide.QtGui import QApplication, QImage, QPainter, QPushButton
from PySide.QtNetwork import QNetworkReply
from PySide.QtWebKit import QWebPage
argument_parser = argparse.ArgumentParser(description="Webpage thumbnailer")
argument_parser.add_argument("url")
argument_parser.add_argument("--width", type=int)
argument_parser.add_argument("--height", type=int)
argument_parser.add_argument("--out", default="out.png")
class Thumbnailer(QObject):
finished = Signal(unicode)
def __init__(self, url, out, width, height):
super(Thumbnailer, self).__init__()
self.url = url
self.out = out
self.width = width
self.height = height
self.reply = None
self.page = QWebPage(self)
self.page.mainFrame().setScrollBarPolicy(
Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self.page.mainFrame().setScrollBarPolicy(
Qt.Vertical, Qt.ScrollBarAlwaysOff)
self.page.loadFinished.connect(self.on_page_finished)
self.page.networkAccessManager().finished.connect(
self.on_network_finished)
self.page.mainFrame().load(url)
def on_page_finished(self, ok):
if ok:
self.render()
if self.reply is None:
# FIXME: on_network_finished() is not called.
# May be url is invalid (e.g. port >= 65535).
# How to get error for it?
error = "Invalid Request Error"
elif self.reply.error() == QNetworkReply.NoError:
error = None
else:
error = self.reply.error().name
self.finished.emit(error)
def on_network_finished(self, reply):
self.reply = reply
def render(self):
self.page.setViewportSize(self.page.mainFrame().contentsSize())
image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
self.page.mainFrame().render(painter)
painter.end()
if self.width is None and self.height is None:
outimg = image
elif self.width is None:
width = int(image.width() * (self.height / image.height()))
outimg = image.scaled(width, self.height)
elif self.height is None:
height = int(image.height() * (self.width / image.width()))
outimg = image.scaled(self.width, height)
else:
scaled = image.scaled(self.width, self.height,
Qt.KeepAspectRatioByExpanding)
outimg = scaled.copy(0, 0, self.width, self.height)
outimg.save(self.out)
def on_finished(error):
if error is None:
QApplication.exit(0)
else:
sys.stderr.write("{0}\n".format(error))
QApplication.exit(1)
def main():
args = argument_parser.parse_args()
app = QApplication(sys.argv)
# When error happend and no window created, cannot exit application
# without kill.
quitbutton = QPushButton("quit")
quitbutton.setWindowTitle("webthumbnail")
quitbutton.clicked.connect(QApplication.quit)
quitbutton.show()
thumbnailer = Thumbnailer(args.url, args.out, args.width, args.height)
thumbnailer.finished.connect(on_finished)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment