Skip to content

Commit

Permalink
Added option to render on transparent background
Browse files Browse the repository at this point in the history
  • Loading branch information
troelskn committed Aug 25, 2010
1 parent 3ae4322 commit e787683
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions webkit2png.py
Expand Up @@ -119,7 +119,7 @@ def __init__(self,**kwargs):
# Not that your desktop must be large enough for # Not that your desktop must be large enough for
# fitting the whole window. # fitting the whole window.
self.grabWholeWindow = kwargs.get('grabWholeWindow', False) self.grabWholeWindow = kwargs.get('grabWholeWindow', False)

self.renderTransparentBackground = kwargs.get('renderTransparentBackground', False)


# Set some default options for QWebPage # Set some default options for QWebPage
self.qWebSettings = { self.qWebSettings = {
Expand Down Expand Up @@ -234,21 +234,32 @@ def render(self, url):
#self._window.repaint() #self._window.repaint()
while QApplication.hasPendingEvents(): while QApplication.hasPendingEvents():
QApplication.processEvents() QApplication.processEvents()

if self.grabWholeWindow:
# Note that this does not fully ensure that the
# window still has the focus when the screen is
# grabbed. This might result in a race condition.
self._view.activateWindow()
image = QPixmap.grabWindow(self._window.winId())
else:
image = QPixmap.grabWidget(self._window)


## Another possible drawing solution if self.renderTransparentBackground:
#image = QImage(self._page.viewportSize(), QImage.Format_ARGB32) # Another possible drawing solution
#painter = QPainter(image) image = QImage(self._page.viewportSize(), QImage.Format_ARGB32)
#self._page.mainFrame().render(painter) image.fill(QColor(255,0,0,0).rgba())
#painter.end()
# http://ariya.blogspot.com/2009/04/transparent-qwebview-and-qwebpage.html
palette = self._view.palette()
palette.setBrush(QPalette.Base, Qt.transparent)
self._page.setPalette(palette)
self._view.setAttribute(Qt.WA_OpaquePaintEvent, False)

painter = QPainter(image)
painter.setBackgroundMode(Qt.TransparentMode)
self._page.mainFrame().render(painter)
painter.end()
else:
if self.grabWholeWindow:
# Note that this does not fully ensure that the
# window still has the focus when the screen is
# grabbed. This might result in a race condition.
self._view.activateWindow()
image = QPixmap.grabWindow(self._window.winId())
else:
image = QPixmap.grabWidget(self._window)



return self._post_process_image(image) return self._post_process_image(image)


Expand Down Expand Up @@ -396,6 +407,8 @@ def init_qtgui(display=None, style=None, qtargs=[]):
help="Time before the request will be canceled [default: %default]", metavar="SECONDS") help="Time before the request will be canceled [default: %default]", metavar="SECONDS")
parser.add_option("-W", "--window", dest="window", action="store_true", parser.add_option("-W", "--window", dest="window", action="store_true",
help="Grab whole window instead of frame (may be required for plugins)", default=False) help="Grab whole window instead of frame (may be required for plugins)", default=False)
parser.add_option("-T", "--transparent", dest="transparent", action="store_true",
help="Render output on a transparent background (Be sure to have a transparent background defined in the html)", default=False)
parser.add_option("", "--style", dest="style", parser.add_option("", "--style", dest="style",
help="Change the Qt look and feel to STYLE (e.G. 'windows').", metavar="STYLE") help="Change the Qt look and feel to STYLE (e.G. 'windows').", metavar="STYLE")
parser.add_option("-d", "--display", dest="display", parser.add_option("-d", "--display", dest="display",
Expand Down Expand Up @@ -459,6 +472,7 @@ def __main_qt():
renderer.wait = options.wait renderer.wait = options.wait
renderer.format = options.format renderer.format = options.format
renderer.grabWholeWindow = options.window renderer.grabWholeWindow = options.window
renderer.renderTransparentBackground = options.transparent


if options.scale: if options.scale:
renderer.scaleRatio = options.ratio renderer.scaleRatio = options.ratio
Expand Down

0 comments on commit e787683

Please sign in to comment.