Skip to content

Commit 4e625af

Browse files
committed
Merge branch 'master' of github.com:troelskn/python-webkit2png into troelskn-master
2 parents 3588ad7 + e787683 commit 4e625af

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

webkit2png.py

+29-15
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(self,**kwargs):
119119
# Not that your desktop must be large enough for
120120
# fitting the whole window.
121121
self.grabWholeWindow = kwargs.get('grabWholeWindow', False)
122-
122+
self.renderTransparentBackground = kwargs.get('renderTransparentBackground', False)
123123

124124
# Set some default options for QWebPage
125125
self.qWebSettings = {
@@ -234,21 +234,32 @@ def render(self, url):
234234
#self._window.repaint()
235235
while QApplication.hasPendingEvents():
236236
QApplication.processEvents()
237-
238-
if self.grabWholeWindow:
239-
# Note that this does not fully ensure that the
240-
# window still has the focus when the screen is
241-
# grabbed. This might result in a race condition.
242-
self._view.activateWindow()
243-
image = QPixmap.grabWindow(self._window.winId())
244-
else:
245-
image = QPixmap.grabWidget(self._window)
246237

247-
## Another possible drawing solution
248-
#image = QImage(self._page.viewportSize(), QImage.Format_ARGB32)
249-
#painter = QPainter(image)
250-
#self._page.mainFrame().render(painter)
251-
#painter.end()
238+
if self.renderTransparentBackground:
239+
# Another possible drawing solution
240+
image = QImage(self._page.viewportSize(), QImage.Format_ARGB32)
241+
image.fill(QColor(255,0,0,0).rgba())
242+
243+
# http://ariya.blogspot.com/2009/04/transparent-qwebview-and-qwebpage.html
244+
palette = self._view.palette()
245+
palette.setBrush(QPalette.Base, Qt.transparent)
246+
self._page.setPalette(palette)
247+
self._view.setAttribute(Qt.WA_OpaquePaintEvent, False)
248+
249+
painter = QPainter(image)
250+
painter.setBackgroundMode(Qt.TransparentMode)
251+
self._page.mainFrame().render(painter)
252+
painter.end()
253+
else:
254+
if self.grabWholeWindow:
255+
# Note that this does not fully ensure that the
256+
# window still has the focus when the screen is
257+
# grabbed. This might result in a race condition.
258+
self._view.activateWindow()
259+
image = QPixmap.grabWindow(self._window.winId())
260+
else:
261+
image = QPixmap.grabWidget(self._window)
262+
252263

253264
return self._post_process_image(image)
254265

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

463477
if options.scale:
464478
renderer.scaleRatio = options.ratio

0 commit comments

Comments
 (0)