<s> __version__ = "<STR_LIT>" <EOL> import Image , ImageFile , ImagePalette <EOL> def i16 ( c ) : <EOL> return ord ( c [ <NUM_LIT:0> ] ) + ( ord ( c [ <NUM_LIT:1> ] ) << <NUM_LIT:8> ) <EOL> def o16 ( i ) : <EOL> return chr ( i & <NUM_LIT:255> ) + chr ( i >> <NUM_LIT:8> & <NUM_LIT:255> ) <EOL> def _accept ( prefix ) : <EOL> return prefix [ : <NUM_LIT:6> ] in [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class GifImageFile ( ImageFile . ImageFile ) : <EOL> format = "<STR_LIT>" <EOL> format_description = "<STR_LIT>" <EOL> global_palette = None <EOL> def data ( self ) : <EOL> s = self . fp . read ( <NUM_LIT:1> ) <EOL> if s and ord ( s ) : <EOL> return self . fp . read ( ord ( s ) ) <EOL> return None <EOL> def _open ( self ) : <EOL> s = self . fp . read ( <NUM_LIT> ) <EOL> if s [ : <NUM_LIT:6> ] not in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise SyntaxError , "<STR_LIT>" <EOL> self . info [ "<STR_LIT:version>" ] = s [ : <NUM_LIT:6> ] <EOL> self . size = i16 ( s [ <NUM_LIT:6> : ] ) , i16 ( s [ <NUM_LIT:8> : ] ) <EOL> self . tile = [ ] <EOL> flags = ord ( s [ <NUM_LIT:10> ] ) <EOL> bits = ( flags & <NUM_LIT:7> ) + <NUM_LIT:1> <EOL> if flags & <NUM_LIT> : <EOL> self . info [ "<STR_LIT>" ] = ord ( s [ <NUM_LIT:11> ] ) <EOL> p = self . fp . read ( <NUM_LIT:3> << bits ) <EOL> for i in range ( <NUM_LIT:0> , len ( p ) , <NUM_LIT:3> ) : <EOL> if not ( chr ( i / <NUM_LIT:3> ) == p [ i ] == p [ i + <NUM_LIT:1> ] == p [ i + <NUM_LIT:2> ] ) : <EOL> p = ImagePalette . raw ( "<STR_LIT>" , p ) <EOL> self . global_palette = self . palette = p <EOL> break <EOL> self . __fp = self . fp <EOL> self . __rewind = self . fp . tell ( ) <EOL> self . seek ( <NUM_LIT:0> ) <EOL> def seek ( self , frame ) : <EOL> if frame == <NUM_LIT:0> : <EOL> self . __offset = <NUM_LIT:0> <EOL> self . dispose = None <EOL> self . __frame = - <NUM_LIT:1> <EOL> self . __fp . seek ( self . __rewind ) <EOL> if frame != self . __frame + <NUM_LIT:1> : <EOL> raise ValueError , "<STR_LIT>" % frame <EOL> self . __frame = frame <EOL> self . tile = [ ] <EOL> self . fp = self . __fp <EOL> if self . __offset : <EOL> self . fp . seek ( self . __offset ) <EOL> while self . data ( ) : <EOL> pass <EOL> self . __offset = <NUM_LIT:0> <EOL> if self . dispose : <EOL> self . im = self . dispose <EOL> self . dispose = None <EOL> self . palette = self . global_palette <EOL> while <NUM_LIT:1> : <EOL> s = self . fp . read ( <NUM_LIT:1> ) <EOL> if not s or s == "<STR_LIT:;>" : <EOL> break <EOL> elif s == "<STR_LIT:!>" : <EOL> s = self . fp . read ( <NUM_LIT:1> ) <EOL> block = self . data ( ) <EOL> if ord ( s ) == <NUM_LIT> : <EOL> flags = ord ( block [ <NUM_LIT:0> ] ) <EOL> if flags & <NUM_LIT:1> : <EOL> self . info [ "<STR_LIT>" ] = ord ( block [ <NUM_LIT:3> ] ) <EOL> self . info [ "<STR_LIT>" ] = i16 ( block [ <NUM_LIT:1> : <NUM_LIT:3> ] ) * <NUM_LIT:10> <EOL> try : <EOL> if flags & <NUM_LIT:8> : <EOL> self . dispose = Image . core . fill ( "<STR_LIT:P>" , self . size , <EOL> self . info [ "<STR_LIT>" ] ) <EOL> elif flags & <NUM_LIT:16> : <EOL> self . dispose = self . im . copy ( ) <EOL> except ( AttributeError , KeyError ) : <EOL> pass <EOL> elif ord ( s ) == <NUM_LIT:255> : <EOL> self . info [ "<STR_LIT>" ] = block , self . fp . tell ( ) <EOL> if block [ : <NUM_LIT:11> ] == "<STR_LIT>" : <EOL> block = self . data ( ) <EOL> if len ( block ) >= <NUM_LIT:3> and ord ( block [ <NUM_LIT:0> ] ) == <NUM_LIT:1> : <EOL> self . info [ "<STR_LIT>" ] = i16 ( block [ <NUM_LIT:1> : <NUM_LIT:3> ] ) <EOL> while self . data ( ) : <EOL> pass <EOL> elif s == "<STR_LIT:U+002C>" : <EOL> s = self . fp . read ( <NUM_LIT:9> ) <EOL> x0 , y0 = i16 ( s [ <NUM_LIT:0> : ] ) , i16 ( s [ <NUM_LIT:2> : ] ) <EOL> x1 , y1 = x0 + i16 ( s [ <NUM_LIT:4> : ] ) , y0 + i16 ( s [ <NUM_LIT:6> : ] ) <EOL> flags = ord ( s [ <NUM_LIT:8> ] ) <EOL> interlace = ( flags & <NUM_LIT:64> ) != <NUM_LIT:0> <EOL> if flags & <NUM_LIT> : <EOL> bits = ( flags & <NUM_LIT:7> ) + <NUM_LIT:1> <EOL> self . palette = ImagePalette . raw ( "<STR_LIT>" , self . fp . read ( <NUM_LIT:3> << bits ) ) <EOL> bits = ord ( self . fp . read ( <NUM_LIT:1> ) ) <EOL> self . __offset = self . fp . tell ( ) <EOL> self . tile = [ ( "<STR_LIT>" , <EOL> ( x0 , y0 , x1 , y1 ) , <EOL> self . __offset , <EOL> ( bits , interlace ) ) ] <EOL> break <EOL> else : <EOL> pass <EOL> if not self . tile : <EOL> raise EOFError , "<STR_LIT>" <EOL> self . mode = "<STR_LIT:L>" <EOL> if self . palette : <EOL> self . mode = "<STR_LIT:P>" <EOL> def tell ( self ) : <EOL> return self . __frame <EOL> try : <EOL> import _imaging_gif <EOL> except ImportError : <EOL> _imaging_gif = None <EOL> RAWMODE = { <EOL> "<STR_LIT:1>" : "<STR_LIT:L>" , <EOL> "<STR_LIT:L>" : "<STR_LIT:L>" , <EOL> "<STR_LIT:P>" : "<STR_LIT:P>" , <EOL> } <EOL> def _save ( im , fp , filename ) : <EOL> if _imaging_gif : <EOL> try : <EOL> _imaging_gif . save ( im , fp , filename ) <EOL> return <EOL> except IOError : <EOL> pass <EOL> try : <EOL> rawmode = RAWMODE [ im . mode ] <EOL> imOut = im <EOL> except KeyError : <EOL> if Image . getmodebase ( im . mode ) == "<STR_LIT>" : <EOL> imOut = im . convert ( "<STR_LIT:P>" ) <EOL> rawmode = "<STR_LIT:P>" <EOL> else : <EOL> imOut = im . convert ( "<STR_LIT:L>" ) <EOL> rawmode = "<STR_LIT:L>" <EOL> for s in getheader ( imOut , im . encoderinfo ) : <EOL> fp . write ( s ) <EOL> flags = <NUM_LIT:0> <EOL> try : <EOL> interlace = im . encoderinfo [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> interlace = <NUM_LIT:1> <EOL> if min ( im . size ) < <NUM_LIT:16> : <EOL> interlace = <NUM_LIT:0> <EOL> if interlace : <EOL> flags = flags | <NUM_LIT:64> <EOL> try : <EOL> transparency = im . encoderinfo [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> fp . write ( "<STR_LIT:!>" + <EOL> chr ( <NUM_LIT> ) + <EOL> chr ( <NUM_LIT:4> ) + <EOL> chr ( <NUM_LIT:1> ) + <EOL> o16 ( <NUM_LIT:0> ) + <EOL> chr ( int ( transparency ) ) <EOL> + chr ( <NUM_LIT:0> ) ) <EOL> fp . write ( "<STR_LIT:U+002C>" + <EOL> o16 ( <NUM_LIT:0> ) + o16 ( <NUM_LIT:0> ) + <EOL> o16 ( im . size [ <NUM_LIT:0> ] ) + <EOL> o16 ( im . size [ <NUM_LIT:1> ] ) + <EOL> chr ( flags ) + <EOL> chr ( <NUM_LIT:8> ) ) <EOL> imOut . encoderconfig = ( <NUM_LIT:8> , interlace ) <EOL> ImageFile . _save ( imOut , fp , [ ( "<STR_LIT>" , ( <NUM_LIT:0> , <NUM_LIT:0> ) + im . size , <NUM_LIT:0> , rawmode ) ] ) <EOL> fp . write ( "<STR_LIT>" ) <EOL> fp . write ( "<STR_LIT:;>" ) <EOL> try : <EOL> fp . flush ( ) <EOL> except : pass <EOL> def _save_netpbm ( im , fp , filename ) : <EOL> import os <EOL> file = im . _dump ( ) <EOL> if im . mode != "<STR_LIT>" : <EOL> os . system ( "<STR_LIT>" % ( file , filename ) ) <EOL> else : <EOL> os . system ( "<STR_LIT>" % ( file , filename ) ) <EOL> try : os . unlink ( file ) <EOL> except : pass <EOL> def getheader ( im , info = None ) : <EOL> """<STR_LIT>""" <EOL> optimize = info and info . get ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> s = [ <EOL> "<STR_LIT>" + <EOL> o16 ( im . size [ <NUM_LIT:0> ] ) + <EOL> o16 ( im . size [ <NUM_LIT:1> ] ) + <EOL> chr ( <NUM_LIT:7> + <NUM_LIT> ) + <EOL> chr ( <NUM_LIT:0> ) + <EOL> chr ( <NUM_LIT:0> ) <EOL> ] <EOL> if optimize : <EOL> i = <NUM_LIT:0> <EOL> maxcolor = <NUM_LIT:0> <EOL> for count in im . histogram ( ) : <EOL> if count : <EOL> maxcolor = i <EOL> i = i + <NUM_LIT:1> <EOL> else : <EOL> maxcolor = <NUM_LIT> <EOL> if im . mode == "<STR_LIT:P>" : <EOL> s . append ( im . im . getpalette ( "<STR_LIT>" ) [ : maxcolor * <NUM_LIT:3> ] ) <EOL> else : <EOL> for i in range ( maxcolor ) : <EOL> s . append ( chr ( i ) * <NUM_LIT:3> ) <EOL> return s <EOL> def getdata ( im , offset = ( <NUM_LIT:0> , <NUM_LIT:0> ) , ** params ) : <EOL> """<STR_LIT>""" <EOL> class collector : <EOL> data = [ ] <EOL> def write ( self , data ) : <EOL> self . data . append ( data ) <EOL> im . load ( ) <EOL> fp = collector ( ) <EOL> try : <EOL> im . encoderinfo = params <EOL> fp . write ( "<STR_LIT:U+002C>" + <EOL> o16 ( offset [ <NUM_LIT:0> ] ) + <EOL> o16 ( offset [ <NUM_LIT:1> ] ) + <EOL> o16 ( im . size [ <NUM_LIT:0> ] ) + <EOL> o16 ( im . size [ <NUM_LIT:1> ] ) + <EOL> chr ( <NUM_LIT:0> ) + <EOL> chr ( <NUM_LIT:8> ) ) <EOL> ImageFile . _save ( im , fp , [ ( "<STR_LIT>" , ( <NUM_LIT:0> , <NUM_LIT:0> ) + im . size , <NUM_LIT:0> , RAWMODE [ im . mode ] ) ] ) <EOL> fp . write ( "<STR_LIT>" ) <EOL> finally : <EOL> del im . encoderinfo <EOL> return fp . data <EOL> Image . register_open ( GifImageFile . format , GifImageFile , _accept ) <EOL> Image . register_save ( GifImageFile . format , _save ) <EOL> Image . register_extension ( GifImageFile . format , "<STR_LIT>" ) <EOL> Image . register_mime ( GifImageFile . format , "<STR_LIT>" ) </s>
