25
25
in supporting documentation or portions thereof, including
26
26
modifications, that you make.
27
27
'''
28
- __all__ = ['MSSLinux' , 'MSSMac' , 'MSSWindows' , 'ScreenshotError' ]
28
+ __all__ = ['mss' , ' MSSLinux' , 'MSSMac' , 'MSSWindows' , 'ScreenshotError' ]
29
29
30
30
from struct import pack
31
31
from platform import system
@@ -588,12 +588,29 @@ def get_pixels(self, monitor):
588
588
return self .image
589
589
590
590
591
+ def mss (* args , ** kwargs ):
592
+ ''' Factory returning a proper MSS class instance.
593
+
594
+ It detects the plateform we are running on
595
+ and choose the most adapted mss_class to take
596
+ screenshots.
597
+
598
+ It then proxies its arguments to the class for
599
+ instantiation.
600
+ '''
601
+
602
+ mss_class = {
603
+ 'Darwin' : MSSMac ,
604
+ 'Linux' : MSSLinux ,
605
+ 'Windows' : MSSWindows
606
+ }[system ()]
607
+
608
+ return mss_class (* args , ** kwargs )
609
+
610
+
591
611
def main ():
592
612
''' Usage example. '''
593
613
594
- systems = {'Darwin' : MSSMac , 'Linux' : MSSLinux , 'Windows' : MSSWindows }
595
- mss = systems [system ()]()
596
-
597
614
def on_exists (fname ):
598
615
''' Callback example when we try to overwrite an existing
599
616
screenshot.
@@ -607,22 +624,24 @@ def on_exists(fname):
607
624
return True
608
625
609
626
try :
610
- print ('One screen shot per monitor' )
611
- for filename in mss .save ():
627
+ screenshotter = mss ()
628
+
629
+ print ('One screenshot per monitor' )
630
+ for filename in screenshotter .save ():
612
631
print (filename )
613
632
614
- print ("\n Screen shot of the monitor 1" )
615
- for filename in mss .save (output = 'monitor-%d.png' , screen = 1 ):
633
+ print ("\n Screenshot of the monitor 1" )
634
+ for filename in screenshotter .save (output = 'monitor-%d.png' , screen = 1 ):
616
635
print (filename )
617
636
618
- print ("\n A shot to grab them all" )
619
- for filename in mss .save (output = 'full-screenshot.png' , screen = - 1 ):
637
+ print ("\n A screenshot to grab them all" )
638
+ for filename in screenshotter .save (output = 'full-screenshot.png' , screen = - 1 ):
620
639
print (filename )
621
640
622
- print ("\n Screen shot of the monitor 1, with callback" )
623
- for filename in mss .save (output = 'mon-%d.png' ,
624
- screen = 1 ,
625
- callback = on_exists ):
641
+ print ("\n Screenshot of the monitor 1, with callback" )
642
+ for filename in screenshotter .save (output = 'mon-%d.png' ,
643
+ screen = 1 ,
644
+ callback = on_exists ):
626
645
print (filename )
627
646
except ScreenshotError as ex :
628
647
print (ex )
0 commit comments