<s> import sys <EOL> import signal <EOL> from PyQt4 . QtCore import * <EOL> from PyQt4 . QtGui import * <EOL> from PyQt4 . QtWebKit import QWebPage <EOL> def save_webpage_screenshot ( url , width , height , file_name = None ) : <EOL> """<STR_LIT>""" <EOL> app = QApplication ( sys . argv ) <EOL> signal . signal ( signal . SIGINT , signal . SIG_DFL ) <EOL> class MyQWebPage ( QWebPage ) : <EOL> @ pyqtSlot ( ) <EOL> def shouldInterruptJavaScript ( qwebpage ) : <EOL> print "<STR_LIT>" <EOL> return False <EOL> webpage = MyQWebPage ( ) <EOL> webpage . setViewportSize ( QSize ( int ( width ) , int ( height ) ) ) <EOL> def print_error ( <EOL> message , <EOL> lineNumber , <EOL> sourceID <EOL> ) : <EOL> print "<STR_LIT>" % locals ( ) <EOL> webpage . javaScriptConsoleMessage = print_error <EOL> if file_name is None : <EOL> result = [ ] <EOL> def onPrintRequested ( virtual_browser_window ) : <EOL> image = QImage ( <EOL> webpage . viewportSize ( ) , <EOL> QImage . Format_ARGB32 <EOL> ) <EOL> painter = QPainter ( image ) <EOL> virtual_browser_window . render ( painter ) <EOL> painter . end ( ) <EOL> if file_name is not None : <EOL> image . save ( file_name ) <EOL> else : <EOL> byte_array = QByteArray ( ) <EOL> buffer = QBuffer ( byte_array ) <EOL> buffer . open ( QIODevice . WriteOnly ) <EOL> image . save ( buffer , "<STR_LIT>" ) <EOL> result . append ( str ( byte_array ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if file_name is None : <EOL> sys . stdout . write ( result [ <NUM_LIT:0> ] ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> else : <EOL> app . quit ( ) <EOL> webpage . printRequested . connect ( onPrintRequested ) <EOL> webpage . mainFrame ( ) . load ( QUrl ( url ) ) <EOL> app . exec_ ( ) <EOL> if file_name is None : <EOL> return result [ <NUM_LIT:0> ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( <EOL> save_webpage_screenshot ( <EOL> * sys . argv [ <NUM_LIT:1> : ] <EOL> ) <EOL> ) </s>
