Skip to content

Develop-Python/python-mss

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A cross-platform multi-screen shot module in pure python using ctypes

Very basic, it will grab one screen shot by monitor or a screen shot of all monitors and save it to a PNG file, Python 2.6/3.5 compatible & PEP8 compliant.

So, while you can pip install --upgrade mss, you may just drop it in your project and forget about it.

MSS stands for Multi-Screen Shot.

It's under zlib licence.

Testing

You can try the MSS module directly from the console:

python mss.py

Instance the good class

You can determine automatically which class to use:

from platform import system
import mss

systems = {
    'Darwin': mss.MSSMac,
    'Linux': mss.MSSLinux,
    'Windows': mss.MSSWindows
    }
mss_class = systems[system()]()

Or simply import the good one:

from mss import MSSLinux as mss_class

save(output, screen, callback)

For each monitor, grab a screen shot and save it to a file.

Parameters:

output - string - the output filename. It can contain '%d' which
                  will be replaced by the monitor number.
screen - integer - grab one screen shot of all monitors (screen=-1)
                   grab one screen shot by monitor (screen=0)
                   grab the screen shot of the monitor N (screen=N)
callback - function - in case where output already exists, call
                      the defined callback function with output
                      as parameter. If it returns True, then
                      continue; else ignores the monitor and
                      switches to ne next.

This is a generator which returns created files.

Examples

One screen shot per monitor:

for filename in mss.save():
    print(filename)

Screen shot of the monitor 1:

for filename in mss.save(screen=1):
    print(filename)

Screen shot of the monitor 1, with callback:

def on_exists(fname):
    ''' Callback example when we try to overwrite an existing
        screen shot.
    '''
    from os import rename
    from os.path import isfile
    if isfile(fname):
        newfile = fname + '.old'
        print('{0} -> {1}'.format(fname, newfile))
        rename(fname, newfile)
    return True

for filename in mss.save(screen=1, callback=on_exists):
    print(filename)

A shot to grab them all (fullscreen shot):

for filename in mss.save(output='fullscreen-shot.png', screen=-1):
    print(filename)

About

An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%