Skip to content

Commit 96f900d

Browse files
author
BoboTiG
committed
Do not overwrite existing image files
1 parent 357d441 commit 96f900d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

mss.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
0.0.2 - add support for python 3 on Windows and GNU/Linux
2626
0.0.3 - remove PNG filters
2727
- remove 'ext' argument, using only PNG
28+
- do not overwrite existing image files
2829
2930
You can always get the latest version of this module at:
3031
@@ -53,6 +54,7 @@
5354

5455
from ctypes.util import find_library
5556
from struct import pack
57+
from os.path import isfile
5658
from platform import system
5759
import sys
5860
import zlib
@@ -63,7 +65,7 @@
6365

6466
elif system() == 'Linux':
6567
from os import environ
66-
from os.path import expanduser, isfile
68+
from os.path import expanduser
6769
import xml.etree.ElementTree as ET
6870
from ctypes import byref, cast, cdll
6971
from ctypes import (
@@ -236,18 +238,21 @@ def save(self, output='mss', oneshot=False):
236238
i += 1
237239
filename += '.png'
238240

239-
pixels = self.get_pixels(monitor)
240-
if pixels is None:
241-
raise ValueError('MSS: no data to process.')
242-
243-
if hasattr(self, 'save_'):
244-
img_out = self.save_(output=filename)
241+
if not isfile(filename):
242+
pixels = self.get_pixels(monitor)
243+
if pixels is None:
244+
raise ValueError('MSS: no data to process.')
245+
246+
if hasattr(self, 'save_'):
247+
img_out = self.save_(output=filename)
248+
else:
249+
img = MSSImage(pixels, monitor[b'width'], monitor[b'height'])
250+
img_out = img.dump(filename)
251+
self.debug('save', 'img_out', img_out)
252+
if img_out is not None:
253+
yield img_out
245254
else:
246-
img = MSSImage(pixels, monitor[b'width'], monitor[b'height'])
247-
img_out = img.dump(filename)
248-
self.debug('save', 'img_out', img_out)
249-
if img_out is not None:
250-
yield img_out
255+
yield filename + ' (already exists)'
251256

252257
def enum_display_monitors(self):
253258
''' Get positions of all monitors.

0 commit comments

Comments
 (0)