def generate_image(gridx, gridy, list_color_dict): color_keys = [list(i.keys()) for i in list_color_dict] img = np.zeros((HEIGHT, WIDTH, 3)) startx = 0 starty = 0 for y in np.append(gridy, HEIGHT): endy = y for x in np.append(gridx, WIDTH): endx = x height = endy - starty width = endx - startx d = r.choice([0, 1, 2]) c1, c2 = r.choice(color_keys[d], size=2, replace=False) img[starty:endy, startx:endx] = generate_patch(height, width, list_color_dict[d], r.choice([1, -1])) startx = endx startx = 0 starty = endy final = data.upscale_nearest(img, ny=UPSCALE_FACTOR, nx=UPSCALE_FACTOR) return final.astype('uint8')
def generate_patch(height, width, color_dict, direction): t = r.choice([0, 1], p=[0.9, 0.1]) # t = r.choice([0,1],p=[1,0]) if t == 0: ### markov stuff pattern = m.RMM([0, 1, 2, 3, 4], self_length=100, sinks=0, reduce_sinks=5) pattern = m.SProg(values=pattern) if direction == 1: sample = m.sample_markov_hierarchy(pattern, width) sample = np.repeat(sample, repeats=r.choice([1, 2, 3, 4], size=width)) sample = sample[:width] patch = np.tile(sample, reps=height) elif direction == -1: sample = m.sample_markov_hierarchy(pattern, height) sample = np.repeat(sample, repeats=r.choice([1, 2, 3, 4, 5], size=height)) sample = sample[:height] patch = np.repeat(sample, repeats=width) patch = patch[:width * height] patch = patch.reshape(height, width) elif t == 1: if direction == 1: patch = r.choice([0, 1, 2], size=width * height) patch = np.repeat(patch, repeats=r.choice([20, 30, 40, 50], size=width * height)) patch = patch[:height * width] patch = patch.reshape(height, width) patch = np.repeat(patch, repeats=r.choice([2, 3, 10], size=height), axis=0) patch = patch[:height, :width] elif direction == -1: patch = r.choice([0, 1, 2], size=width * height) patch = patch.reshape(height, width) patch = np.repeat(patch, repeats=r.choice([2, 3, 4], size=height), axis=0) patch = patch[:height, :width] patch = np.repeat(patch, repeats=r.choice([20, 30, 40, 50], size=width), axis=1) patch = patch[:height, :width] patch = color.replace_indices_with_colors(patch, color_dict) patch[patch < 0] = 0 patch[patch > 255] = 255 return patch
final = cv2.cvtColor(final, cv2.COLOR_HSV2RGB) * 255 return final.astype('uint8') ### GENERATE SECTION print('GENERATE SECTION') for current_iteration in range(N): print('CURRENT_ITERATION:', current_iteration) r.init_def_generator(SEED + current_iteration) # GENERATING THE COLORS FIRST length_choices = [1, 2] lengths_1 = r.choice(length_choices, size=5) lengths_2 = r.choice(length_choices, size=5) color_row_1 = m.Processor(m.RMM(values=[0, 1, 2, 3, 4], self_length=WIDTH, lenghts=lengths_1), length_limit=WIDTH) color_row_2 = m.Processor(m.RMM(values=[10, 11, 12, 13, 14], self_length=WIDTH, lenghts=lengths_2), length_limit=WIDTH) color_parent = m.SProg(values=[color_row_1, color_row_2], start_probs=0) colors = gen_portion(color_parent, 2, WIDTH) print(colors[:, :30])
patterns_1 = [ config.get('pattern_1_a', [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]), config.get('pattern_1_b', [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]), config.get('pattern_1_c', [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]), config.get('pattern_1_d', [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]), config.get('pattern_1_e', [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]), ] patterns_2 = [ config.get('pattern_2_a', [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]), ] patterns_1 = [to_string_pattern(i, skips) for i in patterns_1] patterns_2 = [to_string_pattern(i, skips) for i in patterns_2] # generating the color columns color_patterns = [ m.SPat(pattern=p, self_length=len(p), candidates=skips,lenghts=r.choice([1,2,3,4])) for p in color_patterns_1 for i in range(5) ] color_patterns += [ m.SPat(pattern=p, self_length=len(p), candidates=skips,lenghts=r.choice([5,10,14,15])) for p in color_patterns_2 for i in range(2) ] color_column_left = m.Proc( m.SProg(values=color_patterns, self_length=WIDTH // 3), length_limit=WIDTH) color_column_right = m.Proc( m.SProg(values=color_patterns, self_length=WIDTH // 3), length_limit=WIDTH)
def generate_patch(height, width, color_dict_1, color_dict_2): patch = np.zeros((height, width, 3), dtype='float64') color_start_lengths = np.array( [int(l) for _, (_, l) in color_dict_1.items()]) num_color_samples = width // np.min(color_start_lengths) + 20 pattern = m.FuzzyProgression(values=np.arange(len(color_dict_1)), positive_shifts=3, negative_shifts=3, self_length=num_color_samples) raw_sample = m.sample_markov_hierarchy(pattern, num_color_samples) sample_start = color.replace_indices_with_colors(raw_sample, color_dict_1) sample_end = color.replace_indices_with_colors(raw_sample, color_dict_2) switch = np.array([ r.choice([0, 1], replace=False, size=(2, )) for i in range(sample_start.shape[0]) ]) sample_start_t = np.where(switch[:, 0][:, None], sample_start, sample_end) sample_end_t = np.where(switch[:, 1][:, None], sample_start, sample_end) sample_start = sample_start_t sample_end = sample_end_t start_lengths = color_start_lengths[raw_sample.astype('int32')] start_lengths = np.cumsum(start_lengths) num_vertical_reps = 2 num_vertical_samples = height // num_vertical_reps + 3 model = m.RMM(values=np.arange(0, 41, 5) - 20, self_length=num_vertical_samples) offsets = np.stack([ m.sample_markov_hierarchy(model, num_vertical_samples) for _ in range(num_color_samples) ], axis=1) offsets = np.repeat(offsets, repeats=r.choice( [num_vertical_reps + i for i in range(1)], size=(num_vertical_samples, )), axis=0) offsets = np.cumsum(offsets, axis=0) offsets += start_lengths offsets = np.hstack([np.zeros((offsets.shape[0], 1)), offsets]) i = 0 offset_index = 0 while i < height: current_lengths = offsets[offset_index] acum_max = np.maximum.accumulate(current_lengths) mask = acum_max == current_lengths diff = np.diff(current_lengths[mask]) samples_start_masked = sample_start[mask[1:]] samples_end_masked = sample_end[mask[1:]] p_switch = 0.75 switch = r.choice([0, 1], size=samples_start_masked.shape[0], p=[p_switch, 1 - p_switch]) switch = np.stack((switch, 1 - switch), axis=1) sample_start_switched = np.where(switch[:, 0][:, None], samples_start_masked, samples_end_masked) sample_end_switched = np.where(switch[:, 1][:, None], samples_start_masked, samples_end_masked) multiples = r.choice([20, 25, 35, 50, 60, 70]) gradient = generate_gradient(sample_start_switched, sample_end_switched, diff)[:width] patch[i:i + multiples] = gradient[None, :] i += multiples offset_index += 1 patch[patch < 0] = 0 patch[patch > 255] = 255 return patch
def generate_image(gridx, gridy, list_color_dict): img = np.zeros((HEIGHT, WIDTH, 3), dtype='float64') startx = 0 starty = 0 y_iteration = 0 gridyextended = np.append(gridy, HEIGHT) gridxextended = np.append(gridx, HEIGHT) occupied = np.zeros((gridyextended.size, gridxextended.size), dtype='bool') for i, y in enumerate(gridyextended): endy = y y_iteration += 1 for j, x in enumerate(gridxextended): endx = x if occupied[i, j] > 0: startx = endx continue p = 0.5 elongatey = r.choice([True, False], p=[p, 1 - p]) elongatex = r.choice([True, False], p=[p, 1 - p]) if i >= gridyextended.size - 1: elongatey = False if j >= gridxextended.size - 1: elongatex = False startyactual = starty endyactual = endy startxactual = startx endxactual = endx height = endy - starty width = endx - startx if elongatey: add_height = gridyextended[i + 1] - gridyextended[i] height = endy + add_height - starty endyactual += add_height if elongatex: add_width = gridxextended[j + 1] - gridxextended[j] width = endx + add_width - startx endxactual += add_width if elongatex and elongatey: occupied[i:i + 2, j:j + 2] = True elif elongatex and not elongatey: occupied[i, j:j + 2] = True elif elongatey and not elongatex: occupied[i:i + 2, j] = True else: occupied[i, j] = True patch = generate_patch(height, width, list_color_dict[0], list_color_dict[1]) img[startyactual:endyactual, startxactual:endxactual] = patch startx = endx startx = 0 starty = endy final = data.upscale_nearest(img, ny=UPSCALE_FACTOR, nx=UPSCALE_FACTOR) return final.astype('uint8')