<s> """<STR_LIT>""" <EOL> import sys <EOL> import itertools <EOL> import grapefruit as gf <EOL> from fabulous import utils , xterm256 <EOL> class Image ( object ) : <EOL> """<STR_LIT>""" <EOL> pad = '<STR_LIT:U+0020>' <EOL> def __init__ ( self , path , width = None ) : <EOL> utils . pil_check ( ) <EOL> from PIL import Image as PillsPillsPills <EOL> self . img = PillsPillsPills . open ( path ) <EOL> self . img = self . img . convert ( "<STR_LIT>" ) <EOL> self . resize ( width ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for line in self . reduce ( self . convert ( ) ) : <EOL> if line . strip ( ) : <EOL> yield line <EOL> yield "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:\n>" . join ( self ) <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . img . size <EOL> def resize ( self , width = None ) : <EOL> """<STR_LIT>""" <EOL> ( iw , ih ) = self . size <EOL> if width is None : <EOL> width = min ( iw , utils . term . width ) <EOL> elif isinstance ( width , basestring ) : <EOL> percents = dict ( [ ( pct , '<STR_LIT>' % ( pct ) ) for pct in range ( <NUM_LIT> ) ] ) <EOL> width = percents [ width ] <EOL> height = int ( float ( ih ) * ( float ( width ) / float ( iw ) ) ) <EOL> height //= <NUM_LIT:2> <EOL> self . img = self . img . resize ( ( width , height ) ) <EOL> def reduce ( self , colors ) : <EOL> """<STR_LIT>""" <EOL> need_reset = False <EOL> line = [ ] <EOL> for color , items in itertools . groupby ( colors ) : <EOL> if color is None : <EOL> if need_reset : <EOL> line . append ( "<STR_LIT>" ) <EOL> need_reset = False <EOL> line . append ( self . pad * len ( list ( items ) ) ) <EOL> elif color == "<STR_LIT>" : <EOL> if need_reset : <EOL> line . append ( "<STR_LIT>" ) <EOL> need_reset = False <EOL> yield "<STR_LIT>" . join ( line ) <EOL> else : <EOL> line . pop ( ) <EOL> yield "<STR_LIT>" . join ( line ) <EOL> line = [ ] <EOL> else : <EOL> need_reset = True <EOL> line . append ( "<STR_LIT>" % ( <EOL> color , self . pad * len ( list ( items ) ) ) ) <EOL> def convert ( self ) : <EOL> """<STR_LIT>""" <EOL> ( width , height ) = self . img . size <EOL> bgcolor = utils . term . bgcolor <EOL> self . img . load ( ) <EOL> for y in xrange ( height ) : <EOL> for x in xrange ( width ) : <EOL> rgba = self . img . getpixel ( ( x , y ) ) <EOL> if len ( rgba ) == <NUM_LIT:4> and rgba [ <NUM_LIT:3> ] == <NUM_LIT:0> : <EOL> yield None <EOL> elif len ( rgba ) == <NUM_LIT:3> or rgba [ <NUM_LIT:3> ] == <NUM_LIT:255> : <EOL> yield xterm256 . rgb_to_xterm ( * rgba [ : <NUM_LIT:3> ] ) <EOL> else : <EOL> color = gf . Color . NewFromRgb ( * [ c / <NUM_LIT> for c in rgba ] ) <EOL> rgba = gf . Color . AlphaBlend ( color , bgcolor ) . rgb <EOL> yield xterm256 . rgb_to_xterm ( <EOL> * [ int ( c * <NUM_LIT> ) for c in rgba ] ) <EOL> yield "<STR_LIT>" <EOL> def main ( args ) : <EOL> """<STR_LIT>""" <EOL> for imgpath in args : <EOL> for line in Image ( imgpath ) : <EOL> print line <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
