''' Created on Mar 13, 2016 @author: Brett Paufler (c) Copyright Brett Paufler checkered_tile returns a single XxX img_tile checkered_grid returns a composite grouping of checkered_tiles as a single img 0 is black / wall 1 is white / open ''' import numpy as np from skimage import io from itertools import chain from copy import deepcopy def checkered_tile(pattern='000000000', pixels=10, lines=False): '''Returns an img_tile for use with checkered_grid. img arrays are of arbitrary size (pixels * 30 w*h) '000000000' '111111111' '101010101' black 0 0 0 white 1 1 1 or any combo 1 0 1 0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 1 0 1 pattern: a text string indicating: 0 or 1, from left to right, from top to bottom likely could be a list of anything convertible to an int pixels: size of each part, piece, or sub-tile each tile is a 3x3 checkered_grid, so pixels=10, means a 30x30 img lines: adds gridded black lines to output tile tic-tac-toe style three horizontal, three vertical includes top and left edge (so can mesh together) excludes bottom and right edge ''' #starting with a black tile, subsquares are overwritten as appropriate tile = np.zeros((3 * pixels, 3 * pixels), dtype=float) for N, p in enumerate(pattern): if int(p) == 1: i, j = divmod(N, 3) i, j = i * pixels, j * pixels tile[i:i+pixels, j:j+pixels] = 1.0 #adds black tic-tac-toe lines, if desired if lines: for i in range(3): tile[i*pixels, :] = 0.0 tile[:, i*pixels] = 0.0 return tile def checkered_grid(pattern_list_in, shape, **kwargs): '''Returns an img assembled from pattern_list of checkered_tile pattern_list = list of checkered_tile patterns e.x. ['000000000', '111111111', '101010101'] shape is width x height of img (in tiles) raises exception if width x height != len(pattern_list) **kwargs are any values checkered_tile might take pixels: default 10 (i.e. 30x30) lines: False''' pattern_list = deepcopy(pattern_list_in) #pulls out width/height, confirms len(pattern_list) matches width, height = shape if len(pattern_list) != width * height: raise ValueError('Miss-Match: Length Pattern != Area Shape') #assemble composite img img_high = [] #vertical img stack for _h in range(height): img_wide = [] #horizontal img stack for _w in range(width): pattern = pattern_list.pop(0) img_wide.append(checkered_tile(pattern, **kwargs)) img_high.append(np.hstack(img_wide)) img = np.vstack(img_high) return img def checkered_test(): '''Outputs a few test tiles to ./output/ ''' for p in ['111101111', '111111111', '101010101', '111000000', '100100100']: c = checkered_tile(pattern=p, pixels=25, lines=True ) io.imsave('./output/%s.png' % p, c) #pattern_list = ['010010010', '111010111', '111001111', ''] #numbers solid_dot = ['111101111', '111101111', '111101111', '111101111', '111101111', '111101111'] die_face = ['000010000', '001000100', '100010001', '101000101', '101010101', '101101101'] #pattern_list = die_face img = checkered_grid(die_face, (2,3), lines=True) io.imsave('./output/%s.png' % 'six_wide', img) d_f = list(chain.from_iterable([[d]*20 for d in die_face])) print len(d_f), d_f img = checkered_grid(d_f, (20, 6)) io.imsave('./output/stacked_die_face.png', img) if __name__ == '__main__': checkered_test() die_face = ['000010000', '001000100', '100010001', '101000101', '101010101', '101101101'] df = list(chain.from_iterable([[d]*20 for d in die_face])) print len(df), df #print d_f