Skip to content

Commit 5b6521d

Browse files
author
BoboTiG
committed
Better monitor saving management
1 parent 143a8db commit 5b6521d

2 files changed

Lines changed: 51 additions & 37 deletions

File tree

README.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,26 @@ When initialising an instance of MSS, you can enable debug output::
5050
mss = mss_class(debug=True)
5151

5252

53-
save(output='mss', oneshot=False)
53+
save(output='screenshot', screen=-1)
5454
---------------------------------
5555

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

5858
Parameters::
5959

6060
output - string - the output filename without extension
61-
oneshot - boolean - grab only one screen shot of all monitors
61+
screen - integer - grab one screen shot of all monitors (-1)
62+
grab one screen shot by monitor (0)
63+
grab the screen shot of the monitor $screen
6264

6365
This is a generator which returns created files::
6466

65-
'output-0.png',
66-
'output-1.png',
67+
'screenshot-0.png',
68+
'screenshot-1.png',
6769
...,
68-
'output-N.png'
70+
'screenshot-N.png'
6971
or
70-
'output-full.png'
72+
'screenshot-full.png'
7173

7274

7375
Example
@@ -79,11 +81,15 @@ Then, it is quite simple::
7981

8082
# One screen shot per monitor
8183
for filename in mss.save():
82-
print('File "{}" created.'.format(filename))
84+
print('File: "{}" created.'.format(filename))
85+
86+
# Screen shot of the monitor 1
87+
for filename in mss.save(output='monitor-1', screen=1):
88+
print('File: "{}" created.'.format(filename))
8389

8490
# A shot to grab them all :)
85-
for filename in mss.save(oneshot=True):
86-
print('File "{}" created.'.format(filename))
91+
for filename in mss.save(screen=-1):
92+
print('File: "{}" created.'.format(filename))
8793

8894

8995
Bonus

mss.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ def __init__(self, debug=False):
154154
''' Global vars and class overload. '''
155155

156156
self.DEBUG = debug in [True, 'on' 'yes', 'oui', 1]
157-
self.monitors = []
158-
self.oneshot = False
159157

160158
self.debug('__init__', 'DEBUG', self.DEBUG)
161159
self.init()
@@ -204,40 +202,45 @@ def get_pixels(self, monitor_infos):
204202
'''
205203
return NotImplemented
206204

207-
def save(self, output='mss', oneshot=False):
205+
def save(self, output='screenshot', screen=0):
208206
''' For each monitor, grab a screen shot and save it to a file.
209207
210208
Parameters:
211209
- output - string - the output filename without extension
212-
- oneshot - boolean - grab only one screen shot of all monitors
210+
- screen - integer - grab one screen shot of all monitors (-1)
211+
grab one screen shot by monitor (0)
212+
grab the screen shot of the monitor $screen
213213
214214
This is a generator which returns created files:
215-
'output-0.png',
216-
'output-1.png',
215+
'screenshot-1.png',
216+
'screenshot-2.png',
217217
...,
218-
'output-N.png'
218+
'screenshot-N.png'
219219
or
220-
'output-full.png'
220+
'screenshot-full.png'
221221
'''
222222

223223
self.debug('save')
224-
225-
self.oneshot = oneshot
226-
self.debug('save', 'oneshot', self.oneshot)
224+
self.debug('save', 'screen', screen)
225+
self.debug('save', 'output', output)
227226

228227
# Monitors screen shots!
229-
for i, monitor in enumerate(self.enum_display_monitors()):
228+
for i, monitor in enumerate(self.enum_display_monitors(screen)):
230229
self.debug('save', 'monitor', monitor)
231-
if self.oneshot:
230+
if screen == -1:
232231
filename = '{}-full.png'.format(output)
232+
elif screen == 0:
233+
filename = '{}-{}.png'.format(output, i+1)
233234
else:
234-
filename = '{}-{}.png'.format(output, i)
235-
self.debug('save', 'filename', filename)
236-
self.save_img(data=self.get_pixels(monitor),
237-
width=monitor[b'width'],
238-
height=monitor[b'height'],
239-
output=filename)
240-
yield filename
235+
filename = '{}.png'.format(output)
236+
237+
if screen <= 0 or (screen > 0 and i+1 == screen):
238+
self.debug('save', 'filename', filename)
239+
self.save_img(data=self.get_pixels(monitor),
240+
width=monitor[b'width'],
241+
height=monitor[b'height'],
242+
output=filename)
243+
yield filename
241244

242245
def save_img(self, data, width, height, output):
243246
''' Dump data to the image file.
@@ -292,14 +295,14 @@ def init(self):
292295
''' Mac OSX initialisations '''
293296
self.debug('init')
294297

295-
def enum_display_monitors(self):
298+
def enum_display_monitors(self, screen=-1):
296299
''' Get positions of one or more monitors.
297300
Returns a dict with minimal requirements (see MSS class).
298301
'''
299302

300303
self.debug('enum_display_monitors')
301304

302-
if self.oneshot:
305+
if screen == -1:
303306
rect = CGRectInfinite
304307
yield ({
305308
b'left': int(rect.origin.x),
@@ -510,14 +513,14 @@ def _xfce4_config(self):
510513
b'rotation': rotation
511514
})
512515

513-
def enum_display_monitors(self):
516+
def enum_display_monitors(self, screen=-1):
514517
''' Get positions of one or more monitors.
515518
Returns a dict with minimal requirements (see MSS class).
516519
'''
517520

518521
self.debug('enum_display_monitors')
519522

520-
if self.oneshot:
523+
if screen == -1:
521524
gwa = XWindowAttributes()
522525
self.xlib.XGetWindowAttributes(self.display, self.root, byref(gwa))
523526
yield ({
@@ -620,14 +623,14 @@ def _set_restypes(self):
620623
windll.gdi32.GetDIBits.restypes = INT
621624
windll.gdi32.DeleteObject.restypes = BOOL
622625

623-
def enum_display_monitors(self):
626+
def enum_display_monitors(self, screen=-1):
624627
''' Get positions of one or more monitors.
625628
Returns a dict with minimal requirements (see MSS class).
626629
'''
627630

628631
self.debug('enum_display_monitors')
629632

630-
if self.oneshot:
633+
if screen == -1:
631634
SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN = 76, 77
632635
SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN = 78, 79
633636
left = windll.user32.GetSystemMetrics(SM_XVIRTUALSCREEN)
@@ -751,9 +754,14 @@ def timer(msg):
751754
for filename in mss.save():
752755
print(' File: {}'.format(filename))
753756

757+
# Screen shot of the monitor 1
758+
with timer('Monitor 1 '):
759+
for filename in mss.save(output='monitor-1', screen=1):
760+
print(' File: {}'.format(filename))
761+
754762
# A shot to grab them all :)
755-
with timer('Oneshot=True'):
756-
for filename in mss.save(oneshot=True):
763+
with timer('All in one '):
764+
for filename in mss.save(screen=-1):
757765
print(' File: {}'.format(filename))
758766
return 0
759767

0 commit comments

Comments
 (0)