|
| 1 | +********************************************************************** |
| 2 | +A cross-platform multi-screen shot module in pure python using ctypes |
| 3 | +********************************************************************** |
| 4 | + |
| 5 | +Very basic, it will grab one screen shot by monitor or a screen shot of all monitors and save it to an optimised/progressive PNG/JPEG file, Python 2.7/3.3 compatible. |
| 6 | + |
| 7 | +So, while you can `pip install mss`, you may just drop it in your project and forget about it. |
| 8 | + |
| 9 | +MSS stands for Multi-Screen Shot. |
| 10 | + |
| 11 | +It's under zlib licence. |
| 12 | + |
| 13 | + |
| 14 | +Instance the good class |
| 15 | +======================== |
| 16 | + |
| 17 | +You can determine automatically which class to use:: |
| 18 | + |
| 19 | + from platform import system |
| 20 | + from mss import * |
| 21 | + |
| 22 | + systems = { |
| 23 | + 'Darwin' : MSSMac, |
| 24 | + 'Linux' : MSSLinux, |
| 25 | + 'Windows': MSSWindows |
| 26 | + } |
| 27 | + try: |
| 28 | + MSS = systems[system()] |
| 29 | + except KeyError: |
| 30 | + err = 'System "{0}" not implemented.'.format(system()) |
| 31 | + raise NotImplementedError(err) |
| 32 | + |
| 33 | +Or simply import the good one:: |
| 34 | + |
| 35 | + from mss import MSSLinux as MSS |
| 36 | + |
| 37 | + |
| 38 | +init(debug=False) |
| 39 | +----------------- |
| 40 | + |
| 41 | +When initalising an instance of MSS, you can enable debug output:: |
| 42 | + |
| 43 | + mss = MSS(debug=True) |
| 44 | + |
| 45 | + |
| 46 | +save(output='mss', oneshot=False, ext='png', ftype=0) |
| 47 | +----------------------------------------------------- |
| 48 | + |
| 49 | +For each monitor, grab a screen shot and save it to a file. |
| 50 | + |
| 51 | +Parameters:: |
| 52 | + |
| 53 | + output - string - the output filename without extension |
| 54 | + oneshot - boolean - grab only one screen shot of all monitors |
| 55 | + ext - string - file format to save |
| 56 | + ftype - int - PNG filter type (0..4 [slower]) |
| 57 | + |
| 58 | +This is a generator which returns created files:: |
| 59 | + |
| 60 | + 'output-1.ext', |
| 61 | + 'output-2.ext', |
| 62 | + ..., |
| 63 | + 'output-NN.ext' |
| 64 | + or |
| 65 | + 'output-full.ext' |
| 66 | + |
| 67 | + |
| 68 | +Example |
| 69 | +======== |
| 70 | + |
| 71 | +Then, it is quite simple:: |
| 72 | + |
| 73 | + try: |
| 74 | + mss = MSS() |
| 75 | + |
| 76 | + # One screen shot per monitor |
| 77 | + for filename in mss.save(): |
| 78 | + print('File "{0}" created.'.format(filename)) |
| 79 | + |
| 80 | + # A shot to grab them all :) |
| 81 | + for filename in mss.save(oneshot=True): |
| 82 | + print('File "{0}" created.'.format(filename)) |
| 83 | + except Exception as ex: |
| 84 | + print(ex) |
| 85 | + raise |
| 86 | + |
| 87 | + |
| 88 | +Bonus |
| 89 | +====== |
| 90 | + |
| 91 | +Just for fun ... |
| 92 | +Show us your screen shot with all monitors in one file, we will update the gallery. |
| 93 | + |
| 94 | +Link to the galley: https://tiger-222.fr/tout/python-mss/galerie/ |
0 commit comments