def _grab(to_file, childprocess=False, backend=None, bbox=None, filename=None): if childprocess: log.debug('running "%s" in child process' % backend) if not to_file: f = tempfile.NamedTemporaryFile( suffix='.png', prefix='pyscreenshot_childprocess_') filename = f.name params = ["'%s'" % (filename), 'childprocess=False'] if backend: params += ["backend='%s'" % (backend)] params = ','.join(params) EasyProcess([ sys.executable, '-c', "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % (params), ]).check() if not to_file: im = Image.open(filename) if bbox: im = im.crop(bbox) return im else: if backend: BackendLoader().force(backend) backend_obj = BackendLoader().selected() if to_file: return backend_obj.grab_to_file(filename) else: return backend_obj.grab(bbox)
def _grab(to_file, childprocess=False, backend=None, bbox=None, filename=None): if childprocess: log.debug('running "%s" in child process' % backend) if not to_file: f = tempfile.NamedTemporaryFile( suffix='.png', prefix='pyscreenshot_childprocess_') filename = f.name params = ["'%s'" % (filename), 'childprocess=False'] if backend: params += ["backend='%s'" % (backend)] params = ','.join(params) EasyProcess([sys.executable, '-c', "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % ( params), ]).check() if not to_file: im = Image.open(filename) if bbox: im = im.crop(bbox) return im else: if backend: BackendLoader().force(backend) backend_obj = BackendLoader().selected() if to_file: return backend_obj.grab_to_file(filename) else: return backend_obj.grab(bbox)
def grab(bbox=None, childprocess=False, backend=None): '''Copy the contents of the screen to PIL image memory. :param bbox: optional bounding box :param childprocess: pyscreenshot can cause an error, if it is used on more different virtual displays and back-end is not in different process. Some back-ends are always different processes: scrot, imagemagick :param backend: back-end can be forced if set (examples:scrot, wx,..), otherwise back-end is automatic ''' if childprocess: f = tempfile.NamedTemporaryFile( suffix='.png', prefix='pyscreenshot_childprocess_') filename = f.name params = ["'%s'" % (filename), 'childprocess=False'] if backend: params += ["backend='%s'" % (backend)] params = ','.join(params) EasyProcess(['python', '-c', "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % params, ]).check() im = Image.open(filename) if bbox: im = im.crop(bbox) return im else: if backend: BackendLoader().force(backend) return BackendLoader().selected().grab(bbox)
def print_versions(): print_name('pyscreenshot') print pyscreenshot.__version__ man=BackendLoader() for name in man.all_names: man.force(name) print_name(name) try : x=man.selected() print x.backend_version() except Exception: print 'missing'
def print_versions(): print_name('pyscreenshot') print pyscreenshot.__version__ man = BackendLoader() for name in man.all_names: man.force(name) print_name(name) try: x = man.selected() print x.backend_version() except Exception: print 'missing'
def show(): im = [] backends = BackendLoader().all_names for x in backends: BackendLoader().force(x) try: print 'grabbing by ' + x im.append(ImageGrab.grab(bbox=(100, 200, 300, 400))) except PluginLoaderError as e: print e print im for x in im: x.show() time.sleep(1)
def check_ref(backend, bbox): # some tests fail -> disable return BackendLoader().force(ref) img_ref = pyscreenshot.grab(bbox=bbox) img_debug(img_ref, ref + str(bbox)) BackendLoader().force(backend) im = pyscreenshot.grab(bbox=bbox) img_debug(im, backend + str(bbox)) eq_('RGB', img_ref.mode) eq_('RGB', im.mode) img_diff = ImageChops.difference(img_ref, im) bbox = img_diff.getbbox() if bbox: img_debug(img_diff, 'img_diff' + str(bbox)) eq_(bbox, None, 'different image data %s!=%s bbox=%s' % (ref, backend, bbox))
def run(force_backend, bbox, bgcolor): color = 255 if bgcolor == 'white' else 0 print force_backend, ' ' * (20 - len(force_backend)), if force_backend in SKIP: print 'SKIP' return BackendLoader().force(force_backend) im = pyscreenshot.grab(bbox=bbox) ls = list(im.getdata()) print 'OK' if all([x == color or x == (color, color, color) for x in ls]) else 'FAIL'
def run_all(n, to_file, bbox=None): print print 'n=%s' % n, ', to_file:', to_file,', bounding box:', bbox print '------------------------------------------------------' backends = BackendLoader().all_names for x in backends: try: run(x, n, to_file, bbox) # print 'grabbing by '+x except PluginLoaderError as e: print e
def grab_to_file(filename, childprocess=False, backend=None): '''Copy the contents of the screen to a file. :param filename: file for saving :param childprocess: see :py:func:`grab` :param backend: see :py:func:`grab` ''' if childprocess: params = ["'%s'" % (filename), 'childprocess=False'] if backend: params += ["backend='%s'" % (backend)] params = ','.join(params) EasyProcess(['python', '-c', "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % params, ]).check() else: if backend: BackendLoader().force(backend) return BackendLoader().selected().grab_to_file(filename)
def check_size(backend, bbox): BackendLoader().force(backend) im = pyscreenshot.grab(bbox=bbox) img_debug(im, backend + str(bbox)) if bbox: width = bbox[2] - bbox[0] height = bbox[3] - bbox[1] else: width, height = display_size() eq_(width, im.size[0], 'wrong width! %s' % (backend)) eq_(height, im.size[1], 'wrong height! %s' % (backend))
def run(force_backend, n, to_file, bbox=None): print force_backend,' '*(20-len(force_backend)), BackendLoader().force(force_backend) f = tempfile.NamedTemporaryFile(suffix='.png', prefix='test') filename=f.name start = time.time() for i in range(n): if to_file: pyscreenshot.grab_to_file(filename) else: pyscreenshot.grab(bbox=bbox) end = time.time() print int(10*(end - start))/10.0, 'sec'
def run_all(bgcolor, display, bbox): print print 'bgcolor=', bgcolor print '-------------------------------------' backends = BackendLoader().all_names for x in backends: try: with Display(size=display, bgcolor=bgcolor): # time.sleep(1) try: run(x, bbox, bgcolor=bgcolor) except BackendLoaderError as e: print e except Exception as e: print e
def run(force_backend, n, to_file, bbox=None): print '%-20s' % force_backend, BackendLoader().force(force_backend) f = tempfile.NamedTemporaryFile(suffix='.png', prefix='test') filename = f.name start = time.time() for i in range(n): if to_file: pyscreenshot.grab_to_file(filename) else: pyscreenshot.grab(bbox=bbox) end = time.time() dt = end - start print '%-4.2g sec' % (dt), '(%5d ms per call)' % (1000.0 * dt / n)
def show(backend='auto'): if backend != 'auto': BackendLoader().force(backend) im = grab(bbox=(100, 200, 300, 400)) im.show()
def grab_to_file(filename): '''Copy the contents of the screen to a file. :param filename: file for saving ''' return BackendLoader().selected().grab_to_file(filename)
def test_mix(self): man = BackendLoader() man.force('scrot') man.set_preference(['imagemagick', 'scrot']) eq_(man.selected().name, 'scrot')
def test_force(self): man = BackendLoader() for name in ['imagemagick', 'scrot', 'pygtk', 'pyqt', 'wx']: man.force(name) eq_(man.selected().name, name) man.force(None) # for other tests
def test_pref(self): man = BackendLoader() man.force(None) man.set_preference(['imagemagick', 'scrot']) eq_(man.selected().name, 'imagemagick') man.set_preference(['imagemagick', 'scrot', 'imagemagick']) eq_(man.selected().name, 'imagemagick') man.set_preference(['imagemagick']) eq_(man.selected().name, 'imagemagick') man.set_preference(['pygtk', 'imagemagick', 'scrot']) eq_(man.selected().name, 'pygtk') man.set_preference(['scrot', 'imagemagick']) eq_(man.selected().name, 'scrot') man.set_preference(['scrot', 'imagemagick', 'pygtk']) eq_(man.selected().name, 'scrot') man.set_preference(['scrot', 'imagemagick', 'scrot']) eq_(man.selected().name, 'scrot') man.set_preference(['scrot']) eq_(man.selected().name, 'scrot')
def grab(bbox=None): '''Copy the contents of the screen to PIL image memory. :param bbox: optional bounding box ''' return BackendLoader().selected().grab(bbox)