Don't like ads? PRO users don't see any ads ;-)
Guest

a

By: a guest on Jul 11th, 2012  |  syntax: Python  |  size: 2.81 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # -*- coding: utf-8 -*-
  2.  
  3. """This tries to do more or less the same thing as CutyCapt, but as a
  4. python module.
  5.  
  6. This is a derived work from CutyCapt: http://cutycapt.sourceforge.net/
  7.  
  8. ////////////////////////////////////////////////////////////////////
  9. //
  10. // CutyCapt - A Qt WebKit Web Page Rendering Capture Utility
  11. //
  12. // Copyright (C) 2003-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  13. //
  14. // This program is free software; you can redistribute it and/or
  15. // modify it under the terms of the GNU General Public License
  16. // as published by the Free Software Foundation; either version 2
  17. // of the License, or (at your option) any later version.
  18. //
  19. // This program is distributed in the hope that it will be useful,
  20. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. // GNU General Public License for more details.
  23. //
  24. // $Id$
  25. //
  26. ////////////////////////////////////////////////////////////////////
  27.  
  28. """
  29.  
  30. import sys
  31. from PyQt4 import QtCore, QtGui, QtWebKit
  32.  
  33.  
  34. class Capturer(object):
  35.     """A class to capture webpages as images"""
  36.  
  37.     def __init__(self, url, filename):
  38.         self.url = url
  39.         self.filename = filename
  40.         self.saw_initial_layout = False
  41.         self.saw_document_complete = False
  42.  
  43.     def loadFinishedSlot(self):
  44.         self.saw_document_complete = True
  45.         if self.saw_initial_layout and self.saw_document_complete:
  46.             self.doCapture()
  47.  
  48.     def initialLayoutSlot(self):
  49.         self.saw_initial_layout = True
  50.         if self.saw_initial_layout and self.saw_document_complete:
  51.             self.doCapture()
  52.  
  53.     def capture(self):
  54.         """Captures url as an image to the file specified"""
  55.         self.wb = QtWebKit.QWebPage()
  56.         self.wb.mainFrame().setScrollBarPolicy(
  57.             QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff)
  58.         self.wb.mainFrame().setScrollBarPolicy(
  59.             QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff)
  60.  
  61.         self.wb.loadFinished.connect(self.loadFinishedSlot)
  62.         self.wb.mainFrame().initialLayoutCompleted.connect(
  63.             self.initialLayoutSlot)
  64.  
  65.         self.wb.mainFrame().load(QtCore.QUrl(self.url))
  66.  
  67.     def doCapture(self):
  68.         print "Capturando"
  69.         self.wb.setViewportSize(self.wb.mainFrame().contentsSize())
  70.         img = QtGui.QImage(self.wb.viewportSize(), QtGui.QImage.Format_ARGB32)
  71.         print self.wb.viewportSize()
  72.         painter = QtGui.QPainter(img)
  73.         self.wb.mainFrame().render(painter)
  74.         painter.end()
  75.         img.save(self.filename)
  76.         QtCore.QCoreApplication.instance().quit()
  77.  
  78. if __name__ == "__main__":
  79.     """Run a simple capture"""
  80.     app = QtGui.QApplication(sys.argv)
  81.     c = Capturer(sys.argv[1], sys.argv[2])
  82.     c.capture()
  83.     app.exec_()