2424 0.0.1 - first release
2525 0.0.2 - add support for python 3 on Windows and GNU/Linux
2626 0.0.3 - remove PNG filters
27+ - remove 'ext' argument, using only PNG
2728
2829 You can always get the latest version of this module at:
2930
5859
5960if system () == 'Darwin' :
6061 from Quartz import *
61- from LaunchServices import kUTTypeJPEG , kUTTypePNG
62+ from LaunchServices import kUTTypePNG
6263
6364elif system () == 'Linux' :
6465 from os import environ
@@ -197,21 +198,20 @@ def debug(self, method='', scalar=None, value=None):
197198 else :
198199 print (method + '()' , scalar , type (value ).__name__ , value )
199200
200- def save (self , output = 'mss' , oneshot = False , ext = 'png' ):
201+ def save (self , output = 'mss' , oneshot = False ):
201202 ''' For each monitor, grab a screen shot and save it to a file.
202203
203204 Parameters:
204205 - output - string - the output filename without extension
205206 - oneshot - boolean - grab only one screen shot of all monitors
206- - ext - string - file format to save
207207
208208 This is a generator which returns created files:
209- 'output-1.ext ',
210- 'output-2.ext ',
209+ 'output-1.png ',
210+ 'output-2.png ',
211211 ...,
212- 'output-NN.ext '
212+ 'output-NN.png '
213213 or
214- 'output-full.ext '
214+ 'output-full.png '
215215 '''
216216
217217 self .debug ('save' )
@@ -220,7 +220,6 @@ def save(self, output='mss', oneshot=False, ext='png'):
220220 self .monitors = self .enum_display_monitors () or []
221221
222222 self .debug ('save' , 'oneshot' , self .oneshot )
223- self .debug ('save' , 'extension' , ext )
224223
225224 if len (self .monitors ) < 1 :
226225 raise ValueError ('MSS: no monitor found.' )
@@ -235,16 +234,17 @@ def save(self, output='mss', oneshot=False, ext='png'):
235234 else :
236235 filename = output + '-' + str (i )
237236 i += 1
237+ filename += '.png'
238238
239239 pixels = self .get_pixels (monitor )
240240 if pixels is None :
241241 raise ValueError ('MSS: no data to process.' )
242242
243243 if hasattr (self , 'save_' ):
244- img_out = self .save_ (output = filename , ext = ext )
244+ img_out = self .save_ (output = filename )
245245 else :
246246 img = MSSImage (pixels , monitor [b'width' ], monitor [b'height' ])
247- img_out = img .dump (filename , ext = ext )
247+ img_out = img .dump (filename )
248248 self .debug ('save' , 'img_out' , img_out )
249249 if img_out is not None :
250250 yield img_out
@@ -344,28 +344,22 @@ def get_pixels(self, monitor):
344344 kCGNullWindowID , kCGWindowImageDefault )
345345 return 1
346346
347- def save_ (self , output , ext , * args , ** kargs ):
347+ def save_ (self , output ):
348348 ''' Special method to not use MSSImage class. Speedy. '''
349349
350350 self .debug ('save_' )
351351
352- ext = ext .lower ()
353- type_ = {'jpg' : kUTTypeJPEG , 'jpeg' : kUTTypeJPEG , 'png' : kUTTypePNG }
354- if ext not in ['jpg' , 'jpeg' , 'png' ]:
355- ext = 'png'
356-
357- filename = output + '.' + ext
358352 dpi = 72
359- url = NSURL .fileURLWithPath_ (filename )
360- dest = CGImageDestinationCreateWithURL (url , type_ [ ext ] , 1 , None )
353+ url = NSURL .fileURLWithPath_ (output )
354+ dest = CGImageDestinationCreateWithURL (url , kUTTypePNG , 1 , None )
361355 properties = {
362356 kCGImagePropertyDPIWidth : dpi ,
363357 kCGImagePropertyDPIHeight : dpi ,
364358 }
365359 CGImageDestinationAddImage (dest , self .image , properties )
366360 if not CGImageDestinationFinalize (dest ):
367- filename = None
368- return filename
361+ output = None
362+ return output
369363
370364
371365class MSSLinux (MSS ):
@@ -706,12 +700,6 @@ class MSSImage(object):
706700 ''' This is a class to save data (raw pixels) to a picture file.
707701 '''
708702
709- # Known extensions
710- ext_ok = [
711- #'libjpg', # ctypes over libjpeg
712- 'png' , # pure python PNG implementation
713- ]
714-
715703 def __init__ (self , data = None , width = 1 , height = 1 ):
716704 ''' This method is light and should not change. It is like this
717705 to allow the call of extensions() without manipulating
@@ -725,56 +713,27 @@ def __init__(self, data=None, width=1, height=1):
725713 self .width = int (width )
726714 self .height = int (height )
727715
728- def extensions (self ):
729- ''' List all known and working extensions. '''
730-
731- exts = []
732- for ext in self .ext_ok :
733- if hasattr (self , ext ):
734- exts .append (ext )
735- return exts
736-
737- def dump (self , output = None , ext = 'png' , quality = 80 ):
716+ def dump (self , output = None ):
738717 ''' Dump data to the image file using file format specified by ext.
739718 Returns to created file name if success, else None.
740719 '''
741720
742721 if self .data is None :
743722 raise ValueError ('MSSImage: no data to process.' )
744723
745- self .filename = output
746- self .ext = ext
747- self .quality = max (0 , min (int (quality ), 100 ))
748- contents = None
749-
750- if not hasattr (self , self .ext ):
751- err = 'MSSImage: {0}() not implemented. ' .format (self .ext )
752- err += 'Check MSSImage.extensions() to have more informations.'
753- raise ValueError (err )
754-
755- contents = getattr (self , self .ext )()
756- if contents is not None :
757- self .filename += '.' + self .ext
758- with open (self .filename , 'wb' ) as fileh :
724+ contents = self .png ()
725+ if contents :
726+ with open (output , 'wb' ) as fileh :
759727 fileh .write (contents )
760- return self . filename
728+ return output
761729 return None
762730
763- def libjpg (self ):
764- ''' JPEG implementation using ctypes over libjpeg.
765- '''
766-
767- self .ext = 'jpg'
768- print ('ctypes over libjpeg still not implemented.' )
769- pass
770-
771731 def png (self ):
772732 ''' Pure python PNG implementation.
773733 Image represented as RGB tuples, no interlacing.
774734 http://inaps.org/journal/comment-fonctionne-le-png
775735 '''
776736
777- self .ext = 'png'
778737 width , height , data = self .width , self .height , self .data
779738 to_take = (width * 3 + 3 ) & - 4
780739 padding = 0 if to_take % 8 == 0 else (to_take % 8 ) // 2
0 commit comments