<s> from operator import itemgetter <EOL> import sys , os , argparse , platform <EOL> from PIL import Image , ImageFilter <EOL> """<STR_LIT>""" <EOL> class Gradify ( ) : <EOL> """<STR_LIT>""" <EOL> BROWSER_PREFIXES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> DEMO_CMD = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , black_sensitivity = <NUM_LIT> , white_sensitivity = <NUM_LIT:3> , num_colors = <NUM_LIT:4> , resize = <NUM_LIT> , uniformness = <NUM_LIT:7> , webkit_only = False ) : <EOL> self . MAX_COLORS = num_colors <EOL> self . RESIZE_VAL = resize <EOL> self . UNIFORMNESS = uniformness <EOL> self . spread_quadrants = True <EOL> self . pairs = [ ] <EOL> self . num_done = <NUM_LIT:0> <EOL> if webkit_only : <EOL> self . BROWSER_PREFIXES = [ "<STR_LIT>" ] <EOL> self . IGNORED_COLORS = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> "<STR_LIT>" : black_sensitivity <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> "<STR_LIT>" : white_sensitivity <EOL> } <EOL> } <EOL> self . init_CLI_args ( ) <EOL> if self . args . demo : <EOL> self . demo_file = "<STR_LIT>" <EOL> open ( self . demo_file , "<STR_LIT:w>" ) . close ( ) <EOL> if self . args . file : <EOL> self . get_file ( self . args . file ) <EOL> if self . args . dir : <EOL> self . get_dir ( self . args . dir ) <EOL> if not self . args . file : <EOL> self . get_dir ( "<STR_LIT>" ) <EOL> if self . args . demo : <EOL> platformName = platform . system ( ) <EOL> if platformName in self . DEMO_CMD : <EOL> os . system ( self . DEMO_CMD [ platformName ] % self . demo_file ) <EOL> else : <EOL> os . system ( "<STR_LIT>" + self . demo_file ) <EOL> else : <EOL> self . printRules ( ) <EOL> def init_CLI_args ( self ) : <EOL> self . parser = argparse . ArgumentParser ( ) <EOL> self . parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> self . parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> self . parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> self . parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> self . parser . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> self . parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> self . args = self . parser . parse_args ( ) <EOL> return <EOL> def get_directions ( self , fn ) : <EOL> foo = Image . open ( fn ) <EOL> foo = foo . resize ( ( <NUM_LIT:100> , <NUM_LIT:100> ) , Image . ANTIALIAS ) <EOL> self . image = foo <EOL> col = self . get_colors ( ) <EOL> if self . args . single : <EOL> if self . args . demo : <EOL> self . printExampleCSS ( col ) <EOL> self . pairs . append ( ( self . num_done , col ) ) <EOL> return <EOL> quad_cols = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> taken = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> cols_quads = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> for i in range ( len ( col ) ) : <EOL> count = <NUM_LIT:0> <EOL> a = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> for pix in foo . getdata ( ) : <EOL> if self . get_RGB_diff ( pix , col [ i ] ) < <NUM_LIT> : <EOL> if int ( ( count % <NUM_LIT:100> ) / <NUM_LIT:50> ) == <NUM_LIT:1> : <EOL> a [ <NUM_LIT:2> ] += <NUM_LIT:1> <EOL> else : <EOL> a [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> if int ( ( count / <NUM_LIT> ) / <NUM_LIT:50> ) == <NUM_LIT:0> : <EOL> a [ <NUM_LIT:3> ] += <NUM_LIT:1> <EOL> else : <EOL> a [ <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> count += <NUM_LIT:1> <EOL> cols_quads [ i ] = a <EOL> while <NUM_LIT:0> in taken and not self . args . spread : <EOL> best_quad = a . index ( max ( a ) ) <EOL> if max ( a ) == <NUM_LIT:0> : <EOL> best_quad = taken . index ( <NUM_LIT:0> ) <EOL> if taken [ best_quad ] == <NUM_LIT:0> : <EOL> taken [ best_quad ] = <NUM_LIT:1> <EOL> col [ i ] = list ( col [ i ] ) <EOL> col [ i ] . append ( best_quad * <NUM_LIT> ) <EOL> quad_cols [ i ] = col [ i ] <EOL> break <EOL> else : <EOL> a [ best_quad ] = <NUM_LIT:0> <EOL> if self . args . spread : <EOL> quad_cols = self . calculate_spread ( cols_quads , col ) <EOL> if self . args . demo : <EOL> self . printExampleCSS ( quad_cols ) <EOL> self . pairs . append ( ( self . num_done , quad_cols ) ) <EOL> def calculate_spread ( self , spread_quads , col ) : <EOL> strength_spread = [ ] <EOL> quad_cols = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> taken_col = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> taken_quads = [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> for quad in spread_quads : <EOL> top = quad [ <NUM_LIT:3> ] * <NUM_LIT:1.0> / ( quad [ <NUM_LIT:1> ] + <NUM_LIT> ) <EOL> left = quad [ <NUM_LIT:2> ] * <NUM_LIT:1.0> / ( quad [ <NUM_LIT:0> ] + <NUM_LIT> ) <EOL> if left < <NUM_LIT:1> : <EOL> left = <NUM_LIT:1> / ( left + <NUM_LIT> ) <EOL> if top < <NUM_LIT:1> : <EOL> top = <NUM_LIT:1> / ( top + <NUM_LIT> ) <EOL> strength_spread . append ( top ) <EOL> strength_spread . append ( left ) <EOL> while <NUM_LIT:0> in taken_col : <EOL> best_col = int ( strength_spread . index ( max ( strength_spread ) ) / <NUM_LIT:2> ) <EOL> if max ( strength_spread ) is <NUM_LIT:0> : <EOL> best_col = taken_col . index ( <NUM_LIT:0> ) <EOL> if taken_col [ best_col ] is <NUM_LIT:0> : <EOL> best_quad = spread_quads [ best_col ] . index ( max ( spread_quads [ best_col ] ) ) <EOL> if max ( spread_quads [ best_col ] ) is <NUM_LIT:0> : <EOL> sys . stderr . write ( str ( spread_quads ) ) <EOL> best_quad = taken_quads . index ( <NUM_LIT:0> ) <EOL> if taken_quads [ best_quad ] is <NUM_LIT:0> : <EOL> taken_quads [ best_quad ] = <NUM_LIT:1> <EOL> taken_col [ best_col ] = <NUM_LIT:1> <EOL> col [ best_col ] = list ( col [ best_col ] ) <EOL> col [ best_col ] . append ( best_quad * <NUM_LIT> ) <EOL> quad_cols [ best_col ] = col [ best_col ] <EOL> spread_quads [ best_col ] [ best_quad ] = <NUM_LIT:0> <EOL> strength_spread [ strength_spread . index ( max ( strength_spread ) ) ] = <NUM_LIT:0> <EOL> return quad_cols <EOL> def printRules ( self ) : <EOL> for pair in self . pairs : <EOL> i = <NUM_LIT:0> <EOL> if self . args . classname : <EOL> print ( "<STR_LIT>" % ( self . args . classname , pair [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> print ( "<STR_LIT>" % ( pair [ <NUM_LIT:0> ] ) ) <EOL> print ( "<STR_LIT>" ) <EOL> if self . args . single : <EOL> print "<STR_LIT>" + str ( pair [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( pair [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( pair [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] ) + "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" + str ( pair [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( pair [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( pair [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) + "<STR_LIT>" <EOL> if not self . args . single : <EOL> print ( "<STR_LIT>" ) <EOL> for prefix in self . BROWSER_PREFIXES : <EOL> for color in pair [ <NUM_LIT:1> ] : <EOL> print prefix + "<STR_LIT>" + str ( color [ <NUM_LIT:3> ] ) + "<STR_LIT>" + str ( color [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:2> ] ) + "<STR_LIT:U+002C>" + str ( <NUM_LIT:1> ) + "<STR_LIT>" + str ( color [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:2> ] ) + "<STR_LIT>" <EOL> i += <NUM_LIT:1> <EOL> if i == self . MAX_COLORS : <EOL> print "<STR_LIT:;>" <EOL> break <EOL> print "<STR_LIT:U+002C>" <EOL> print ( "<STR_LIT:}>" ) <EOL> def get_file ( self , filen ) : <EOL> self . image = Image . open ( filen ) <EOL> self . imageFileName = filen <EOL> self . get_directions ( filen ) <EOL> def get_dir ( self , dir ) : <EOL> dir = dir + "<STR_LIT:/>" <EOL> self . num_files = len ( os . listdir ( dir ) ) <EOL> for fn in os . listdir ( dir ) : <EOL> fn = dir + fn <EOL> if os . path . isfile ( fn ) and "<STR_LIT>" in fn : <EOL> self . image = Image . open ( fn ) <EOL> self . imageFileName = fn <EOL> self . get_directions ( fn ) <EOL> self . num_done += <NUM_LIT:1> <EOL> sys . stderr . write ( "<STR_LIT>" + str ( self . num_done ) + "<STR_LIT:/>" + str ( self . num_files ) + "<STR_LIT>" ) <EOL> def printExampleCSS ( self , colors ) : <EOL> i = <NUM_LIT:0> <EOL> with open ( self . demo_file , "<STR_LIT:a>" ) as df : <EOL> df . write ( "<STR_LIT>" ) <EOL> if self . args . single : <EOL> sys . stderr . write ( str ( colors ) ) <EOL> df . write ( "<STR_LIT>" + str ( colors [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( colors [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( colors [ <NUM_LIT:2> ] ) + "<STR_LIT>" ) <EOL> else : <EOL> for prefix in self . BROWSER_PREFIXES : <EOL> for color in colors : <EOL> df . write ( prefix + "<STR_LIT>" + str ( color [ <NUM_LIT:3> ] ) + "<STR_LIT>" + str ( color [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:2> ] ) + "<STR_LIT:U+002C>" + str ( <NUM_LIT:1> ) + "<STR_LIT>" + str ( color [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( color [ <NUM_LIT:2> ] ) + "<STR_LIT>" ) <EOL> i += <NUM_LIT:1> <EOL> if i == self . MAX_COLORS : <EOL> break <EOL> df . write ( "<STR_LIT:U+002C>" ) <EOL> df . write ( "<STR_LIT:;>" ) <EOL> df . write ( "<STR_LIT>" ) <EOL> df . write ( "<STR_LIT>" + self . imageFileName + "<STR_LIT>" ) <EOL> df . write ( "<STR_LIT>" ) <EOL> def printShowCase ( self , colors ) : <EOL> i = <NUM_LIT:0> <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" + str ( self . numDone ) + "<STR_LIT>" <EOL> for color in reversed ( colors ) : <EOL> i += <NUM_LIT:1> <EOL> if ( i == self . MAX_COLORS ) : <EOL> break <EOL> print "<STR_LIT>" + self . imageFileName + "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def get_colors ( self ) : <EOL> self . image = self . image . resize ( ( <NUM_LIT> , <NUM_LIT> ) , Image . ANTIALIAS ) <EOL> h = self . image . filter ( ImageFilter . BLUR ) <EOL> h = h . histogram ( ) <EOL> self . colors = [ ] <EOL> r = h [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> g = h [ <NUM_LIT> : <NUM_LIT> * <NUM_LIT:2> ] <EOL> b = h [ <NUM_LIT> * <NUM_LIT:2> : <NUM_LIT> * <NUM_LIT:3> ] <EOL> self . ranked_colors = sorted ( self . image . getcolors ( self . image . size [ <NUM_LIT:0> ] * self . image . size [ <NUM_LIT:1> ] ) , key = itemgetter ( <NUM_LIT:0> ) ) <EOL> for i in range ( len ( self . ranked_colors ) ) : <EOL> self . colors . append ( self . ranked_colors [ len ( self . ranked_colors ) - <NUM_LIT:1> - i ] ) <EOL> if self . MAX_COLORS is <NUM_LIT:1> : <EOL> return self . colors [ <NUM_LIT:0> ] <EOL> else : <EOL> return self . findBestColors ( ) <EOL> def get_RGB_diff ( self , old , new ) : <EOL> return abs ( <NUM_LIT> * abs ( old [ <NUM_LIT:0> ] - new [ <NUM_LIT:0> ] ) ** ( <NUM_LIT:1> / <NUM_LIT> ) + <NUM_LIT> * abs ( old [ <NUM_LIT:1> ] - new [ <NUM_LIT:1> ] ) ** ( <NUM_LIT:1> / <NUM_LIT> ) + <NUM_LIT> * abs ( old [ <NUM_LIT:2> ] - new [ <NUM_LIT:2> ] ) ** ( <NUM_LIT:1> / <NUM_LIT> ) ) ** ( <NUM_LIT:1> / <NUM_LIT> ) <EOL> def find_single_color ( self ) : <EOL> for i in range ( len ( self . colors ) ) : <EOL> diffB = self . get_RGB_diff ( self . IGNORED_COLORS [ "<STR_LIT>" ] [ "<STR_LIT>" ] , self . colors [ i ] [ <NUM_LIT:1> ] ) <EOL> diffW = self . get_RGB_diff ( self . IGNORED_COLORS [ "<STR_LIT>" ] [ "<STR_LIT>" ] , self . colors [ i ] [ <NUM_LIT:1> ] ) <EOL> if diffB > <NUM_LIT:4> and diffW > <NUM_LIT> : <EOL> sys . stderr . write ( str ( diffB ) + "<STR_LIT:\n>" ) <EOL> sys . stderr . write ( str ( self . colors [ i ] [ <NUM_LIT:1> ] ) + "<STR_LIT:\n>" ) <EOL> return self . colors [ i ] [ <NUM_LIT:1> ] <EOL> return self . colors [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def findBestColors ( self ) : <EOL> selectedColors = [ ] <EOL> bad_color = False <EOL> sensitivity = self . UNIFORMNESS <EOL> ignored_radius = <NUM_LIT:0> <EOL> if self . args . single : <EOL> return self . find_single_color ( ) <EOL> while ( len ( selectedColors ) < self . MAX_COLORS ) : <EOL> selectedColors = [ ] <EOL> for i in range ( len ( self . colors ) ) : <EOL> bad_color = False <EOL> for col , col_dict in self . IGNORED_COLORS . iteritems ( ) : <EOL> diff = self . get_RGB_diff ( col_dict [ "<STR_LIT>" ] , self . colors [ i ] [ <NUM_LIT:1> ] ) <EOL> if diff < col_dict [ "<STR_LIT>" ] - ignored_radius : <EOL> bad_color = True <EOL> break <EOL> for j in range ( len ( selectedColors ) ) : <EOL> diff = self . get_RGB_diff ( self . colors [ i ] [ <NUM_LIT:1> ] , selectedColors [ j ] ) <EOL> if ( diff < sensitivity ) : <EOL> bad_color = True <EOL> break <EOL> if bad_color : <EOL> continue <EOL> selectedColors . append ( self . colors [ i ] [ <NUM_LIT:1> ] ) <EOL> if ignored_radius < <NUM_LIT:2> : <EOL> ignored_radius += <NUM_LIT:1> <EOL> else : <EOL> sensitivity -= <NUM_LIT:1> <EOL> ignored_radius = <NUM_LIT:0> <EOL> return selectedColors [ <NUM_LIT:0> : <NUM_LIT:4> ] <EOL> foo = Gradify ( ) </s>
