@@ -119,7 +119,7 @@ def __init__(self,**kwargs):
119
119
# Not that your desktop must be large enough for
120
120
# fitting the whole window.
121
121
self .grabWholeWindow = kwargs .get ('grabWholeWindow' , False )
122
-
122
+ self . renderTransparentBackground = kwargs . get ( 'renderTransparentBackground' , False )
123
123
124
124
# Set some default options for QWebPage
125
125
self .qWebSettings = {
@@ -234,21 +234,32 @@ def render(self, url):
234
234
#self._window.repaint()
235
235
while QApplication .hasPendingEvents ():
236
236
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 )
246
237
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
+
252
263
253
264
return self ._post_process_image (image )
254
265
@@ -396,6 +407,8 @@ def init_qtgui(display=None, style=None, qtargs=[]):
396
407
help = "Time before the request will be canceled [default: %default]" , metavar = "SECONDS" )
397
408
parser .add_option ("-W" , "--window" , dest = "window" , action = "store_true" ,
398
409
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 )
399
412
parser .add_option ("" , "--style" , dest = "style" ,
400
413
help = "Change the Qt look and feel to STYLE (e.G. 'windows')." , metavar = "STYLE" )
401
414
parser .add_option ("-d" , "--display" , dest = "display" ,
@@ -459,6 +472,7 @@ def __main_qt():
459
472
renderer .wait = options .wait
460
473
renderer .format = options .format
461
474
renderer .grabWholeWindow = options .window
475
+ renderer .renderTransparentBackground = options .transparent
462
476
463
477
if options .scale :
464
478
renderer .scaleRatio = options .ratio
0 commit comments