Skip to content

Commit

Permalink
bugfix for wx pyscreenshot backend
Browse files Browse the repository at this point in the history
  • Loading branch information
titi committed Nov 2, 2012
1 parent c13b5fd commit 33796e9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 18 deletions.
25 changes: 25 additions & 0 deletions pyvirtualdisplay/examples/screenshot4.py
@@ -0,0 +1,25 @@
'''
two calls
'''
import logging
logging.basicConfig(level=logging.DEBUG)

from easyprocess import EasyProcess
from pyvirtualdisplay.smartdisplay import SmartDisplay

backend1 = 'wx'
backend2 = 'wx'


with SmartDisplay(visible=0, bgcolor='black') as disp:
disp.pyscreenshot_backend = backend1
with EasyProcess('xmessage test1'):
img1 = disp.waitgrab()

with SmartDisplay(visible=0, bgcolor='black') as disp:
disp.pyscreenshot_backend = backend2
with EasyProcess('xmessage test2'):
img2 = disp.waitgrab()

img1.show()
img2.show()
44 changes: 26 additions & 18 deletions pyvirtualdisplay/smartdisplay.py
Expand Up @@ -9,13 +9,15 @@
log = logging.getLogger(__name__)


#class DisplayError(Exception):
# class DisplayError(Exception):
# pass

class DisplayTimeoutError(Exception):
pass


class SmartDisplay(Display):
pyscreenshot_backend = None
def autocrop(self, im):
'''Crop borders off an image.
Expand All @@ -30,14 +32,20 @@ def autocrop(self, im):
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
return None # no contents
return None # no contents

def grab(self, autocrop=True):
img=pyscreenshot.grab()
try:
# first try newer pyscreenshot version
img = pyscreenshot.grab(childprocess=1, backend=self.pyscreenshot_backend)
except TypeError:
# try older pyscreenshot version
img = pyscreenshot.grab()

if autocrop:
img = self.autocrop(img)
return img

def waitgrab(self, timeout=60, autocrop=True, cb_imgcheck=None):
'''start process and create screenshot.
Repeat screenshot until it is not empty and
Expand All @@ -51,7 +59,7 @@ def waitgrab(self, timeout=60, autocrop=True, cb_imgcheck=None):
False = reject img
'''
t = 0
sleep_time = 0.3 # for fast windows
sleep_time = 0.3 # for fast windows
repeat_time = 1
while 1:
log.debug('sleeping %s secs' % str(sleep_time))
Expand All @@ -64,25 +72,25 @@ def waitgrab(self, timeout=60, autocrop=True, cb_imgcheck=None):
if cb_imgcheck(img):
break
sleep_time = repeat_time
repeat_time += 1 # progressive
repeat_time += 1 # progressive
if t > timeout:
msg = 'Timeout! elapsed time:%s timeout:%s ' % (t, timeout)
raise DisplayTimeoutError(msg)
raise DisplayTimeoutError(msg)
break

log.debug('screenshot is empty, next try..')
assert img
# if not img:
# log.debug('screenshot is empty!')
return img











50 changes: 50 additions & 0 deletions tests/test_smart2.py
@@ -0,0 +1,50 @@
from easyprocess import EasyProcess
from nose.tools import eq_
from pyvirtualdisplay.smartdisplay import SmartDisplay, DisplayTimeoutError
from unittest import TestCase
from path import path
import pyscreenshot


class Test(TestCase):
def check_double(self, backend1, backend2=None):
if not backend2:
backend2 = backend1

with SmartDisplay(visible=0, bgcolor='black') as disp:
disp.pyscreenshot_backend = backend1
with EasyProcess('xmessage hello1'):
img = disp.waitgrab()
eq_(img is not None, True)

with SmartDisplay(visible=0, bgcolor='black') as disp:
disp.pyscreenshot_backend = backend2
with EasyProcess('xmessage hello2'):
img = disp.waitgrab()
eq_(img is not None, True)

def test_double_wx(self):
self.check_double('wx')

def test_double_pygtk(self):
self.check_double('pygtk')

def test_double_pyqt(self):
self.check_double('pyqt')

def test_double_imagemagick(self):
self.check_double('imagemagick')

def test_double_scrot(self):
self.check_double('scrot')

def test_double_wx_pygtk(self):
self.check_double('wx', 'pygtk')

def test_double_wx_pyqt(self):
self.check_double('wx', 'pyqt')

def test_double_pygtk_pyqt(self):
self.check_double('pygtk', 'pyqt')


0 comments on commit 33796e9

Please sign in to comment.