<s> import hashlib <EOL> from io import BytesIO <EOL> from PIL import Image , ImageDraw <EOL> import binascii <EOL> class Generator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rows , columns , digest = hashlib . md5 , foreground = [ "<STR_LIT>" ] , background = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> entropy_provided = len ( digest ( b"<STR_LIT:test>" ) . hexdigest ( ) ) // <NUM_LIT:2> * <NUM_LIT:8> <EOL> entropy_required = ( columns // <NUM_LIT:2> + columns % <NUM_LIT:2> ) * rows + <NUM_LIT:8> <EOL> if entropy_provided < entropy_required : <EOL> raise ValueError ( "<STR_LIT>" % ( str ( digest ) , entropy_required ) ) <EOL> self . digest_entropy = entropy_provided <EOL> self . rows = rows <EOL> self . columns = columns <EOL> self . foreground = foreground <EOL> self . background = background <EOL> self . digest = digest <EOL> def _get_bit ( self , n , hash_bytes ) : <EOL> """<STR_LIT>""" <EOL> if hash_bytes [ n // <NUM_LIT:8> ] >> int ( <NUM_LIT:8> - ( ( n % <NUM_LIT:8> ) + <NUM_LIT:1> ) ) & <NUM_LIT:1> == <NUM_LIT:1> : <EOL> return True <EOL> return False <EOL> def _generate_matrix ( self , hash_bytes ) : <EOL> """<STR_LIT>""" <EOL> half_columns = self . columns // <NUM_LIT:2> + self . columns % <NUM_LIT:2> <EOL> cells = self . rows * half_columns <EOL> matrix = [ [ False ] * self . columns for _ in range ( self . rows ) ] <EOL> for cell in range ( cells ) : <EOL> if self . _get_bit ( cell , hash_bytes [ <NUM_LIT:1> : ] ) : <EOL> column = cell // self . columns <EOL> row = cell % self . rows <EOL> matrix [ row ] [ column ] = True <EOL> matrix [ row ] [ self . columns - column - <NUM_LIT:1> ] = True <EOL> return matrix <EOL> def _data_to_digest_byte_list ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if len ( data ) // <NUM_LIT:2> == self . digest_entropy // <NUM_LIT:8> : <EOL> try : <EOL> binascii . unhexlify ( data . encode ( '<STR_LIT:utf-8>' ) ) <EOL> digest = data . encode ( '<STR_LIT:utf-8>' ) <EOL> except ( TypeError ) : <EOL> digest = self . digest ( data . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> except ( binascii . Error ) : <EOL> digest = self . digest ( data . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> else : <EOL> digest = self . digest ( data . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> return [ int ( digest [ i * <NUM_LIT:2> : i * <NUM_LIT:2> + <NUM_LIT:2> ] , <NUM_LIT:16> ) for i in range ( <NUM_LIT:16> ) ] <EOL> def _generate_png ( self , matrix , width , height , padding , foreground , background ) : <EOL> """<STR_LIT>""" <EOL> image = Image . new ( "<STR_LIT>" , ( width + padding [ <NUM_LIT:2> ] + padding [ <NUM_LIT:3> ] , height + padding [ <NUM_LIT:0> ] + padding [ <NUM_LIT:1> ] ) , background ) <EOL> draw = ImageDraw . Draw ( image ) <EOL> block_width = width // self . columns <EOL> block_height = height // self . rows <EOL> for row , row_columns in enumerate ( matrix ) : <EOL> for column , cell in enumerate ( row_columns ) : <EOL> if cell : <EOL> x1 = padding [ <NUM_LIT:2> ] + column * block_width <EOL> y1 = padding [ <NUM_LIT:0> ] + row * block_height <EOL> x2 = padding [ <NUM_LIT:2> ] + ( column + <NUM_LIT:1> ) * block_width - <NUM_LIT:1> <EOL> y2 = padding [ <NUM_LIT:0> ] + ( row + <NUM_LIT:1> ) * block_height - <NUM_LIT:1> <EOL> draw . rectangle ( ( x1 , y1 , x2 , y2 ) , fill = foreground ) <EOL> stream = BytesIO ( ) <EOL> image . save ( stream , format = "<STR_LIT>" , optimize = True ) <EOL> image_raw = stream . getvalue ( ) <EOL> stream . close ( ) <EOL> return image_raw <EOL> def _generate_ascii ( self , matrix , foreground , background ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:\n>" . join ( [ "<STR_LIT>" . join ( [ foreground if cell else background for cell in row ] ) for row in matrix ] ) <EOL> def generate ( self , data , width , height , padding = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , output_format = "<STR_LIT>" , inverted = False ) : <EOL> """<STR_LIT>""" <EOL> digest_byte_list = self . _data_to_digest_byte_list ( data ) <EOL> matrix = self . _generate_matrix ( digest_byte_list ) <EOL> if output_format == "<STR_LIT>" : <EOL> background = self . background <EOL> foreground = self . foreground [ digest_byte_list [ <NUM_LIT:0> ] % len ( self . foreground ) ] <EOL> elif output_format == "<STR_LIT:ascii>" : <EOL> foreground = "<STR_LIT:+>" <EOL> background = "<STR_LIT:->" <EOL> if inverted : <EOL> foreground , background = background , foreground <EOL> if output_format == "<STR_LIT>" : <EOL> return self . _generate_png ( matrix , width , height , padding , foreground , background ) <EOL> if output_format == "<STR_LIT:ascii>" : <EOL> return self . _generate_ascii ( matrix , foreground , background ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % output_format ) </s>
