Skip to content

Commit ce3cd0a

Browse files
author
BoboTiG
committed
Add a callback to save() method, in case where 'ouput' already exists
1 parent 9f3a07f commit ce3cd0a

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

README.rst

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Passing the `--debug` argument will make it more verbose.
2323

2424

2525
Instance the good class
26-
========================
26+
=======================
2727

2828
You can determine automatically which class to use::
2929

@@ -50,8 +50,8 @@ When initialising an instance of MSS, you can enable debug output::
5050
mss = mss_class(debug=True)
5151

5252

53-
save(output='screenshot', screen=-1)
54-
---------------------------------
53+
save(output='screenshot', screen=-1, callback=lambda *x: True)
54+
--------------------------------------------------------------
5555
5656
For each monitor, grab a screen shot and save it to a file.
5757

@@ -61,18 +61,23 @@ Parameters::
6161
screen - integer - grab one screen shot of all monitors (screen=-1)
6262
grab one screen shot by monitor (screen=0)
6363
grab the screen shot of the monitor N (screen=N)
64+
callback - function - in case where output already exists, call
65+
the defined callback function with output
66+
as parameter. If it returns True, then
67+
continue; else ignores the monitor and
68+
switches to ne next.
6469

6570
This is a generator which returns created files::
6671

67-
'screenshot-0.png',
6872
'screenshot-1.png',
73+
'screenshot-2.png',
6974
...,
7075
'screenshot-N.png'
7176
or
7277
'screenshot-full.png'
7378

7479

75-
Example
80+
Examples
7681
========
7782

7883
Then, it is quite simple::
@@ -91,9 +96,23 @@ Then, it is quite simple::
9196
for filename in mss.save(screen=-1):
9297
print('File: "{}" created.'.format(filename))
9398

99+
# Example with a callback
100+
def on_exists(fname):
101+
''' Callback example when we try to overwrite an existing screen shot. '''
102+
103+
from os import rename
104+
newfile = fname + '.old'
105+
print('Renaming "{}" to "{}"'.format(fname, newfile))
106+
rename(fname, newfile)
107+
return True
108+
109+
# Screen shot of the monitor 1, with callback
110+
for filename in mss.save(output='monitor-1', screen=1, callback=on_exists):
111+
print('File: "{}" created.'.format(filename))
112+
94113

95114
Bonus
96-
======
115+
=====
97116

98117
Just for fun ...
99118
Show us your screen shot with all monitors in one file, we will update the gallery.

mss.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232

3333
from struct import pack
34+
from os.path import isfile
3435
from platform import system
3536
import sys
3637
import zlib
@@ -45,7 +46,7 @@ class ScreenshotError(Exception):
4546
from LaunchServices import kUTTypePNG
4647
elif system() == 'Linux':
4748
from os import environ
48-
from os.path import expanduser, isfile
49+
from os.path import expanduser
4950
import xml.etree.ElementTree as ET
5051
from ctypes.util import find_library
5152
from ctypes import byref, cast, cdll, POINTER, Structure, \
@@ -202,14 +203,19 @@ def get_pixels(self, monitor_infos):
202203
'''
203204
return NotImplemented
204205

205-
def save(self, output='screenshot', screen=0):
206+
def save(self, output='screenshot', screen=0, callback=lambda *x: True):
206207
''' For each monitor, grab a screen shot and save it to a file.
207208
208209
Parameters:
209210
- output - string - the output filename without extension
210211
- screen - int - grab one screen shot of all monitors (screen=-1)
211212
grab one screen shot by monitor (screen=0)
212213
grab the screen shot of the monitor N (screen=N)
214+
- callback - function - in case where output already exists, call
215+
the defined callback function with output
216+
as parameter. If it returns True, then
217+
continue; else ignores the monitor and
218+
switches to ne next.
213219
214220
This is a generator which returns created files:
215221
'screenshot-1.png',
@@ -235,6 +241,8 @@ def save(self, output='screenshot', screen=0):
235241
filename = '{}.png'.format(output)
236242

237243
if screen <= 0 or (screen > 0 and i+1 == screen):
244+
if isfile(filename) and not callback(filename):
245+
continue
238246
self.debug('save', 'filename', filename)
239247
self.save_img(data=self.get_pixels(monitor),
240248
width=monitor[b'width'],
@@ -763,6 +771,21 @@ def timer(msg):
763771
with timer('All in one '):
764772
for filename in mss.save(screen=-1):
765773
print(' File: {}'.format(filename))
774+
775+
# Example with a callback
776+
def on_exists(fname):
777+
''' Callback example when we try to overwrite an existing screen shot. '''
778+
779+
from os import rename
780+
newfile = fname + '.old'
781+
print(' Renaming {} to {}'.format(fname, newfile))
782+
rename(fname, newfile)
783+
return True
784+
785+
# Screen shot of the monitor 1, with callback
786+
with timer('Monitor 1 '):
787+
for filename in mss.save(output='monitor-1', screen=1, callback=on_exists):
788+
print(' File: {}'.format(filename))
766789
return 0
767790

768791

0 commit comments

Comments
 (0)