Created
April 27, 2014 04:50
-
-
Save arulrajnet/f144d02b82fbc24a235a to your computer and use it in GitHub Desktop.
Generate image of the webpage by just passing the url to this python program.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import argparse | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
from PyQt4.QtWebKit import * | |
__author__ = 'Arul' | |
class ImageOption(): | |
"""docstring for ImageOption""" | |
url = "http://google.com" | |
output_file = "webpage.png" | |
width = 480 | |
height = 320 | |
class Url2Image(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, width=480, height=320): | |
self.load(QUrl(url)) | |
self.wait_load() | |
frame = self.page().mainFrame() | |
settings = self.page().settings() | |
# Remove scroll policy | |
frame.setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) | |
frame.setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) | |
settings.setUserStyleSheetUrl(QUrl("data:text/css,html,body{overflow-y:hidden !important;}")) | |
# set to webpage size | |
#self.page().setViewportSize(frame.contentsSize()) # This will take the full size image of the page | |
self.page().setViewportSize(QSize(width, height)) | |
# 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 | |
def main(self): | |
parser = argparse.ArgumentParser(description='This is a script for url to image by PiQube') | |
parser.add_argument('-u','--url', help='URL of the page',required=True) | |
parser.add_argument('-o','--output',help='Output file name', required=True) | |
parser.add_argument('-W','--width',help='Width of the output image', type=int, required=False) | |
parser.add_argument('-H','--height',help='Height of the output image', type=int, required=False) | |
args = parser.parse_args() | |
if (args.width and args.height): | |
self.capture(args.url, args.output, args.width, args.height) | |
elif (args.width): | |
self.capture(args.url, args.output, width = args.width) | |
elif (args.height): | |
self.capture(args.url, args.output, height = args.height) | |
else: | |
self.capture(args.url, args.output) | |
if __name__ == '__main__': | |
main(Url2Image()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @arulrajnet, Can you help me with the URL2PDF method, your Url2Image works perfectly fine. I am stuck with some issue of generating PDFs, i tried to use your Url2Image functionality to generate PDF but didn't worked for me. Can you help me with that pls?