<s> import Tkinter , Image <EOL> _pilbitmap_ok = None <EOL> def _pilbitmap_check ( ) : <EOL> global _pilbitmap_ok <EOL> if _pilbitmap_ok is None : <EOL> try : <EOL> im = Image . new ( "<STR_LIT:1>" , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> Tkinter . BitmapImage ( data = "<STR_LIT>" % im . im . id ) <EOL> _pilbitmap_ok = <NUM_LIT:1> <EOL> except Tkinter . TclError : <EOL> _pilbitmap_ok = <NUM_LIT:0> <EOL> return _pilbitmap_ok <EOL> class PhotoImage : <EOL> def __init__ ( self , image = None , size = None , ** kw ) : <EOL> if image is None : <EOL> if kw . has_key ( "<STR_LIT:file>" ) : <EOL> image = Image . open ( kw [ "<STR_LIT:file>" ] ) <EOL> del kw [ "<STR_LIT:file>" ] <EOL> elif kw . has_key ( "<STR_LIT:data>" ) : <EOL> from StringIO import StringIO <EOL> image = Image . open ( StringIO ( kw [ "<STR_LIT:data>" ] ) ) <EOL> del kw [ "<STR_LIT:data>" ] <EOL> if hasattr ( image , "<STR_LIT>" ) and hasattr ( image , "<STR_LIT:size>" ) : <EOL> mode = image . mode <EOL> if mode == "<STR_LIT:P>" : <EOL> image . load ( ) <EOL> try : <EOL> mode = image . palette . mode <EOL> except AttributeError : <EOL> mode = "<STR_LIT>" <EOL> size = image . size <EOL> kw [ "<STR_LIT:width>" ] , kw [ "<STR_LIT>" ] = size <EOL> else : <EOL> mode = image <EOL> image = None <EOL> if mode not in [ "<STR_LIT:1>" , "<STR_LIT:L>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> mode = Image . getmodebase ( mode ) <EOL> self . __mode = mode <EOL> self . __size = size <EOL> self . __photo = apply ( Tkinter . PhotoImage , ( ) , kw ) <EOL> self . tk = self . __photo . tk <EOL> if image : <EOL> self . paste ( image ) <EOL> def __del__ ( self ) : <EOL> name = self . __photo . name <EOL> self . __photo . name = None <EOL> try : <EOL> self . __photo . tk . call ( "<STR_LIT:image>" , "<STR_LIT>" , name ) <EOL> except : <EOL> pass <EOL> def __str__ ( self ) : <EOL> return str ( self . __photo ) <EOL> def width ( self ) : <EOL> return self . __size [ <NUM_LIT:0> ] <EOL> def height ( self ) : <EOL> return self . __size [ <NUM_LIT:1> ] <EOL> def paste ( self , im , box = None ) : <EOL> im . load ( ) <EOL> image = im . im <EOL> if image . isblock ( ) and im . mode == self . __mode : <EOL> block = image <EOL> else : <EOL> block = image . new_block ( self . __mode , im . size ) <EOL> image . convert2 ( block , image ) <EOL> tk = self . __photo . tk <EOL> try : <EOL> tk . call ( "<STR_LIT>" , self . __photo , block . id ) <EOL> except Tkinter . TclError , v : <EOL> try : <EOL> import _imagingtk <EOL> try : <EOL> _imagingtk . tkinit ( tk . interpaddr ( ) , <NUM_LIT:1> ) <EOL> except AttributeError : <EOL> _imagingtk . tkinit ( id ( tk ) , <NUM_LIT:0> ) <EOL> tk . call ( "<STR_LIT>" , self . __photo , block . id ) <EOL> except ( ImportError , AttributeError , Tkinter . TclError ) : <EOL> raise <EOL> class BitmapImage : <EOL> def __init__ ( self , image = None , ** kw ) : <EOL> if image is None : <EOL> if kw . has_key ( "<STR_LIT:file>" ) : <EOL> image = Image . open ( kw [ "<STR_LIT:file>" ] ) <EOL> del kw [ "<STR_LIT:file>" ] <EOL> elif kw . has_key ( "<STR_LIT:data>" ) : <EOL> from StringIO import StringIO <EOL> image = Image . open ( StringIO ( kw [ "<STR_LIT:data>" ] ) ) <EOL> del kw [ "<STR_LIT:data>" ] <EOL> self . __mode = image . mode <EOL> self . __size = image . size <EOL> if _pilbitmap_check ( ) : <EOL> image . load ( ) <EOL> kw [ "<STR_LIT:data>" ] = "<STR_LIT>" % image . im . id <EOL> self . __im = image <EOL> else : <EOL> kw [ "<STR_LIT:data>" ] = image . tobitmap ( ) <EOL> self . __photo = apply ( Tkinter . BitmapImage , ( ) , kw ) <EOL> def __del__ ( self ) : <EOL> name = self . __photo . name <EOL> self . __photo . name = None <EOL> try : <EOL> self . __photo . tk . call ( "<STR_LIT:image>" , "<STR_LIT>" , name ) <EOL> except : <EOL> pass <EOL> def width ( self ) : <EOL> return self . __size [ <NUM_LIT:0> ] <EOL> def height ( self ) : <EOL> return self . __size [ <NUM_LIT:1> ] <EOL> def __str__ ( self ) : <EOL> return str ( self . __photo ) <EOL> def getimage ( photo ) : <EOL> photo . tk . call ( "<STR_LIT>" , photo ) <EOL> def _show ( image , title ) : <EOL> class UI ( Tkinter . Label ) : <EOL> def __init__ ( self , master , im ) : <EOL> if im . mode == "<STR_LIT:1>" : <EOL> self . image = BitmapImage ( im , foreground = "<STR_LIT>" , master = master ) <EOL> else : <EOL> self . image = PhotoImage ( im , master = master ) <EOL> Tkinter . Label . __init__ ( self , master , image = self . image , <EOL> bg = "<STR_LIT>" , bd = <NUM_LIT:0> ) <EOL> if not Tkinter . _default_root : <EOL> raise IOError , "<STR_LIT>" <EOL> top = Tkinter . Toplevel ( ) <EOL> if title : <EOL> top . title ( title ) <EOL> UI ( top , image ) . pack ( ) </s>
