<s> import Image <EOL> import os , sys <EOL> _viewers = [ ] <EOL> def register ( viewer , order = <NUM_LIT:1> ) : <EOL> try : <EOL> if issubclass ( viewer , Viewer ) : <EOL> viewer = viewer ( ) <EOL> except TypeError : <EOL> pass <EOL> if order > <NUM_LIT:0> : <EOL> _viewers . append ( viewer ) <EOL> elif order < <NUM_LIT:0> : <EOL> _viewers . insert ( <NUM_LIT:0> , viewer ) <EOL> def show ( image , title = None , ** options ) : <EOL> for viewer in _viewers : <EOL> if viewer . show ( image , title = title , ** options ) : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> class Viewer : <EOL> def show ( self , image , ** options ) : <EOL> if image . mode [ : <NUM_LIT:4> ] == "<STR_LIT>" : <EOL> base = "<STR_LIT:L>" <EOL> else : <EOL> base = Image . getmodebase ( image . mode ) <EOL> if base != image . mode and image . mode != "<STR_LIT:1>" : <EOL> image = image . convert ( base ) <EOL> self . show_image ( image , ** options ) <EOL> format = None <EOL> def get_format ( self , image ) : <EOL> return self . format <EOL> def get_command ( self , file , ** options ) : <EOL> raise NotImplementedError <EOL> def save_image ( self , image ) : <EOL> return image . _dump ( format = self . get_format ( image ) ) <EOL> def show_image ( self , image , ** options ) : <EOL> return self . show_file ( self . save_image ( image ) , ** options ) <EOL> def show_file ( self , file , ** options ) : <EOL> os . system ( self . get_command ( file , ** options ) ) <EOL> return <NUM_LIT:1> <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> class WindowsViewer ( Viewer ) : <EOL> format = "<STR_LIT>" <EOL> def get_command ( self , file , ** options ) : <EOL> return "<STR_LIT>" % ( file , file ) <EOL> register ( WindowsViewer ) <EOL> elif sys . platform == "<STR_LIT>" : <EOL> class MacViewer ( Viewer ) : <EOL> format = "<STR_LIT>" <EOL> def get_command ( self , file , ** options ) : <EOL> command = "<STR_LIT>" <EOL> command = "<STR_LIT>" % ( command , file , file ) <EOL> return command <EOL> register ( MacViewer ) <EOL> else : <EOL> def which ( executable ) : <EOL> path = os . environ . get ( "<STR_LIT>" ) <EOL> if not path : <EOL> return None <EOL> for dirname in path . split ( os . pathsep ) : <EOL> filename = os . path . join ( dirname , executable ) <EOL> if os . path . isfile ( filename ) : <EOL> return filename <EOL> return None <EOL> class UnixViewer ( Viewer ) : <EOL> def show_file ( self , file , ** options ) : <EOL> command , executable = self . get_command_ex ( file , ** options ) <EOL> command = "<STR_LIT>" % ( command , file , file ) <EOL> os . system ( command ) <EOL> return <NUM_LIT:1> <EOL> class DisplayViewer ( UnixViewer ) : <EOL> def get_command_ex ( self , file , ** options ) : <EOL> command = executable = "<STR_LIT>" <EOL> return command , executable <EOL> if which ( "<STR_LIT>" ) : <EOL> register ( DisplayViewer ) <EOL> class XVViewer ( UnixViewer ) : <EOL> def get_command_ex ( self , file , title = None , ** options ) : <EOL> command = executable = "<STR_LIT>" <EOL> if title : <EOL> command = command + "<STR_LIT>" % title <EOL> return command , executable <EOL> if which ( "<STR_LIT>" ) : <EOL> register ( XVViewer ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print show ( Image . open ( sys . argv [ <NUM_LIT:1> ] ) , * sys . argv [ <NUM_LIT:2> : ] ) </s>
