44Source: https://github.com/BoboTiG/python-mss
55"""
66
7+ from datetime import datetime
8+
79from .exception import ScreenShotError
810from .screenshot import ScreenShot
911from .tools import to_png
@@ -59,28 +61,36 @@ def monitors(self):
5961 }
6062
6163 Note: monitor can be a tuple like PIL.Image.grab() accepts,
62- it must be converted to the approriate dict.
64+ it must be converted to the appropriate dict.
6365 """
6466
6567 raise NotImplementedError ('Subclasses need to implement this!' )
6668
67- def save (self , mon = 0 , output = 'monitor-%d .png' , callback = None ):
69+ def save (self , mon = 0 , output = 'monitor-{mon} .png' , callback = None ):
6870 # type: (int, str, Callable[[str], None]) -> Iterator[str]
6971 """
70- Grab a screenshot and save it to a file.
72+ Grab a screen shot and save it to a file.
7173
72- :param int mon: The monitor to screenshot (default=0).
73- -1: grab one screenshot of all monitors
74- 0: grab one screenshot by monitor
75- N: grab the screenshot of the monitor N
74+ :param int mon: The monitor to screen shot (default=0).
75+ -1: grab one screen shot of all monitors
76+ 0: grab one screen shot by monitor
77+ N: grab the screen shot of the monitor N
7678
7779 :param str output: The output filename.
78- %d, if present, will be replaced by
79- the monitor number.
8080
81- :param callable callback: Callback called before saving
82- the screenshot to a file.
83- Take the ``output`` argument as parameter.
81+ It can take several keywords to customize the filename:
82+ - `{mon}`: the monitor number
83+ - `{top}`: the screen shot y-coordinate of the upper-left corner
84+ - `{left}`: the screen shot x-coordinate of the upper-left corner
85+ - `{width}`: the screen shot's width
86+ - `{height}`: the screen shot's height
87+ - `{date}`: the current date using the default formatter
88+
89+ As it is using the `format()` function, you can specify
90+ formatting options like `{date:%Y-%m-%s}`.
91+
92+ :param callable callback: Callback called before saving the
93+ screen shot to a file. Take the `output` argument as parameter.
8494
8595 :return generator: Created file(s).
8696 """
@@ -90,27 +100,24 @@ def save(self, mon=0, output='monitor-%d.png', callback=None):
90100 raise ScreenShotError ('No monitor found.' )
91101
92102 if mon == 0 :
93- # One screenshot by monitor
94- for i , monitor in enumerate (monitors [1 :], 1 ):
95- fname = output
96- if '%d' in output :
97- fname = output .replace ('%d' , str (i ))
103+ # One screen shot by monitor
104+ for idx , monitor in enumerate (monitors [1 :], 1 ):
105+ fname = output .format (mon = idx , date = datetime .now (), ** monitor )
98106 if callable (callback ):
99107 callback (fname )
100108 sct = self .grab (monitor )
101109 to_png (sct .rgb , sct .size , fname )
102110 yield fname
103111 else :
104- # A screenshot of all monitors together or
105- # a screenshot of the monitor N.
106- mon_number = 0 if mon == - 1 else mon
112+ # A screen shot of all monitors together or
113+ # a screen shot of the monitor N.
114+ mon = 0 if mon == - 1 else mon
107115 try :
108- monitor = monitors [mon_number ]
116+ monitor = monitors [mon ]
109117 except IndexError :
110118 raise ScreenShotError ('Monitor does not exist.' , locals ())
111119
112- if '%d' in output :
113- output = output .replace ('%d' , str (mon_number ))
120+ output = output .format (mon = mon , date = datetime .now (), ** monitor )
114121 if callable (callback ):
115122 callback (output )
116123 sct = self .grab (monitor )
@@ -119,7 +126,7 @@ def save(self, mon=0, output='monitor-%d.png', callback=None):
119126
120127 def shot (self , ** kwargs ):
121128 """
122- Helper to save the screenshot of the 1st monitor, by default.
129+ Helper to save the screen shot of the 1st monitor, by default.
123130 You can pass the same arguments as for ``save``.
124131 """
125132
0 commit comments