<s> import math <EOL> import numpy as np <EOL> from freetype import * <EOL> import matplotlib . pyplot as plt <EOL> def make_label ( text , filename , size = <NUM_LIT:12> , angle = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> face = Face ( filename ) <EOL> face . set_char_size ( size * <NUM_LIT:64> ) <EOL> angle = ( angle / <NUM_LIT> ) * math . pi <EOL> matrix = FT_Matrix ( ( int ) ( math . cos ( angle ) * <NUM_LIT> ) , <EOL> ( int ) ( - math . sin ( angle ) * <NUM_LIT> ) , <EOL> ( int ) ( math . sin ( angle ) * <NUM_LIT> ) , <EOL> ( int ) ( math . cos ( angle ) * <NUM_LIT> ) ) <EOL> flags = FT_LOAD_RENDER <EOL> pen = FT_Vector ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> FT_Set_Transform ( face . _FT_Face , byref ( matrix ) , byref ( pen ) ) <EOL> previous = <NUM_LIT:0> <EOL> xmin , xmax = <NUM_LIT:0> , <NUM_LIT:0> <EOL> ymin , ymax = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for c in text : <EOL> face . load_char ( c , flags ) <EOL> kerning = face . get_kerning ( previous , c ) <EOL> previous = c <EOL> bitmap = face . glyph . bitmap <EOL> pitch = face . glyph . bitmap . pitch <EOL> width = face . glyph . bitmap . width <EOL> rows = face . glyph . bitmap . rows <EOL> top = face . glyph . bitmap_top <EOL> left = face . glyph . bitmap_left <EOL> pen . x += kerning . x <EOL> x0 = ( pen . x >> <NUM_LIT:6> ) + left <EOL> x1 = x0 + width <EOL> y0 = ( pen . y >> <NUM_LIT:6> ) - ( rows - top ) <EOL> y1 = y0 + rows <EOL> xmin , xmax = min ( xmin , x0 ) , max ( xmax , x1 ) <EOL> ymin , ymax = min ( ymin , y0 ) , max ( ymax , y1 ) <EOL> pen . x += face . glyph . advance . x <EOL> pen . y += face . glyph . advance . y <EOL> L = np . zeros ( ( ymax - ymin , xmax - xmin ) , dtype = np . ubyte ) <EOL> previous = <NUM_LIT:0> <EOL> pen . x , pen . y = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for c in text : <EOL> face . load_char ( c , flags ) <EOL> kerning = face . get_kerning ( previous , c ) <EOL> previous = c <EOL> bitmap = face . glyph . bitmap <EOL> pitch = face . glyph . bitmap . pitch <EOL> width = face . glyph . bitmap . width <EOL> rows = face . glyph . bitmap . rows <EOL> top = face . glyph . bitmap_top <EOL> left = face . glyph . bitmap_left <EOL> pen . x += kerning . x <EOL> x = ( pen . x >> <NUM_LIT:6> ) - xmin + left <EOL> y = ( pen . y >> <NUM_LIT:6> ) - ymin - ( rows - top ) <EOL> data = [ ] <EOL> for j in range ( rows ) : <EOL> data . extend ( bitmap . buffer [ j * pitch : j * pitch + width ] ) <EOL> if len ( data ) : <EOL> Z = np . array ( data , dtype = np . ubyte ) . reshape ( rows , width ) <EOL> L [ y : y + rows , x : x + width ] |= Z [ : : - <NUM_LIT:1> , : : <NUM_LIT:1> ] <EOL> pen . x += face . glyph . advance . x <EOL> pen . y += face . glyph . advance . y <EOL> return L <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from PIL import Image <EOL> n_words = <NUM_LIT:100> <EOL> H , W , dpi = <NUM_LIT> , <NUM_LIT> , <NUM_LIT> <EOL> I = np . zeros ( ( H , W , <NUM_LIT:3> ) , dtype = np . ubyte ) <EOL> S = np . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , n_words ) <EOL> S = ( S - S . min ( ) ) / ( S . max ( ) - S . min ( ) ) <EOL> S = np . sort ( <NUM_LIT:1> - np . sqrt ( S ) ) [ : : - <NUM_LIT:1> ] <EOL> sizes = ( <NUM_LIT:12> + S * <NUM_LIT> ) . astype ( int ) . tolist ( ) <EOL> def spiral ( ) : <EOL> eccentricity = <NUM_LIT> <EOL> radius = <NUM_LIT:8> <EOL> step = <NUM_LIT:0.1> <EOL> t = <NUM_LIT:0> <EOL> while True : <EOL> t += step <EOL> yield eccentricity * radius * t * math . cos ( t ) , radius * t * math . sin ( t ) <EOL> fails = <NUM_LIT:0> <EOL> for size in sizes : <EOL> angle = np . random . randint ( - <NUM_LIT> , <NUM_LIT> ) <EOL> L = make_label ( '<STR_LIT>' , '<STR_LIT>' , size , angle = angle ) <EOL> h , w = L . shape <EOL> if h < H and w < W : <EOL> x0 = W // <NUM_LIT:2> + ( np . random . uniform ( ) - <NUM_LIT> ) * <NUM_LIT:50> <EOL> y0 = H // <NUM_LIT:2> + ( np . random . uniform ( ) - <NUM_LIT> ) * <NUM_LIT:50> <EOL> for dx , dy in spiral ( ) : <EOL> c = <NUM_LIT> + <NUM_LIT> * np . random . random ( ) <EOL> x = int ( x0 + dx ) <EOL> y = int ( y0 + dy ) <EOL> if x <= w // <NUM_LIT:2> or y <= h // <NUM_LIT:2> or x >= ( W - w // <NUM_LIT:2> ) or y >= ( H - h // <NUM_LIT:2> ) : <EOL> fails += <NUM_LIT:1> <EOL> break <EOL> if ( I [ y - h // <NUM_LIT:2> : y - h // <NUM_LIT:2> + h , x - w // <NUM_LIT:2> : x - w // <NUM_LIT:2> + w , <NUM_LIT:0> ] * L ) . sum ( ) == <NUM_LIT:0> : <EOL> I [ y - h // <NUM_LIT:2> : y - h // <NUM_LIT:2> + h , x - w // <NUM_LIT:2> : x - w // <NUM_LIT:2> + w , <NUM_LIT:0> ] |= ( c * L ) . astype ( int ) <EOL> I [ y - h // <NUM_LIT:2> : y - h // <NUM_LIT:2> + h , x - w // <NUM_LIT:2> : x - w // <NUM_LIT:2> + w , <NUM_LIT:1> ] |= ( c * L ) . astype ( int ) <EOL> I [ y - h // <NUM_LIT:2> : y - h // <NUM_LIT:2> + h , x - w // <NUM_LIT:2> : x - w // <NUM_LIT:2> + w , <NUM_LIT:2> ] |= ( c * L ) . astype ( int ) <EOL> break <EOL> print ( "<STR_LIT>" . format ( fails ) ) <EOL> fig = plt . figure ( figsize = ( W / dpi , H / dpi ) , dpi = dpi ) <EOL> ax = fig . add_axes ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , frameon = False ) <EOL> ax . imshow ( I , interpolation = '<STR_LIT>' , cmap = plt . cm . gray , origin = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> I = Image . fromarray ( I [ : : - <NUM_LIT:1> , : : <NUM_LIT:1> , : : <NUM_LIT:1> ] , mode = '<STR_LIT>' ) <EOL> I . save ( '<STR_LIT>' ) </s>
