Exemple #1
0
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
Exemple #2
0
def generate_full_image(color_string, seed):
    r.init_def_generator(seed)

    image = np.zeros((HEIGHT, WIDTH, 3))
    plots = []
    loop_key = r.bind_generator()
    setup_key = r.bind_generator()

    post_process = lambda x: data.integrate_series(
        x, n=2, mean_influences=[0, 0])

    pup = m.SimpleProgression(values=1,
                              self_length=[10, 40, 50],
                              post_process=post_process)
    pdown = m.SimpleProgression(values=-1,
                                self_length=[10, 40, 50],
                                post_process=post_process)
    arc = m.RandomMarkovModel(values=[pup, pdown], self_length=[2])

    p = m.RandomMarkovModel(
        # values=[p1, p2, arc],
        values=[arc],
        parent_rkey=r.bind_generator_from(setup_key))

    # for i in range(-30,30):
    for i in range(1):

        sample = m.sample_markov_hierarchy(p, 1000)
        sample = data.integrate_series(sample, 1, mean_influences=1)
        # sample = data.integrate_series(sample,1,mean_influences=0)
        # sample -= np.min(sample)
        # sample = data.integrate_series(sample,1)

        # slices = [ np.s_[:50],np.s_[50:100], np.s_[100:] ]
        # for slice in slices:
        #     sample[slice] -= np.mean(sample[slice])
        # sample = data.integrate_series(sample,1)
        # sample[:60+i] -= np.mean(sample[:60+i])
        # sample[60+i:] -= np.mean(sample[60+i:])
        # sample = data.integrate_series(sample,1)
        plots += [sample]

    plt.plot(plots[0])
    mng = plt.get_current_fig_manager()
    mng.full_screen_toggle()
    plt.show()
    # viz.animate_plots_y(plots)

    return image
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_full_image(color_string,seed):
        r.init_def_generator(seed)

        image = np.zeros((HEIGHT,WIDTH,3))
        plots = []
        loop_key = r.bind_generator()
        setup_key = r.bind_generator()


        p1 = m.MarkovModel(
                values=[0.1,-0.1],
                preference_matrix=data.str2mat('1 5, 5 1'),
                self_length=SEGMENT_LENGTH,
                parent_rkey=r.bind_generator_from(setup_key)

            )

        p2 = m.MarkovModel(
            values=[-0.1, 0.1],
            preference_matrix=data.str2mat('1 5, 5 1'),
            self_length=SEGMENT_LENGTH,
            parent_rkey=r.bind_generator_from(setup_key)

        )

        num_coefs = 12
        vs = np.sin(np.linspace(0, 1, num_coefs) * np.pi * 2) * 0.1
        p3 = m.SimpleProgression(
            values=vs,
            start_probs=0,
            self_length=[num_coefs],
            parent_rkey=r.bind_generator_from(loop_key)
        )

        p = m.MarkovModel(
            values=[p1, p2, p3],
            start_probs=2,
            preference_matrix=data.str2mat(
                '0 1 2, 1 0 2, 1 1 4'),
            self_length=HEIGHT//SEGMENT_LENGTH+1,
            parent_rkey=r.bind_generator_from(setup_key)
        )

        num_samples_1 = HEIGHT//2
        sample_scale_1 = m.sample_markov_hierarchy(p, num_samples_1)
        sample_2 = m.sample_markov_hierarchy(p, num_samples_1)
        sample_3 = m.sample_markov_hierarchy(p, num_samples_1)

        # interpolation_h_1 = integrate_and_normalize(sample_scale_1,2)
        # interpolation_h_2 = integrate_and_normalize(sample_2,2)
        interpolation_color = integrate_and_normalize(sample_3,2)

        color_repo = color.build_color_repository(color_string)
        meta = color.get_meta_from_palette(
            color_repo['First'],
            keys=[0,1,2,3],
            meta_cast_function=int)
        print(meta)
        color_lines = compute_color_lines(color_repo,interpolation_color)
        print(color_lines.shape)

        # plt.plot(interpolation_h_1)
        # plt.plot(interpolation_h_2)
        # plt.plot(interpolation_color)
        # plt.show()


        scale_1_freq = r.choice_from(setup_key,config.get('scale-1-freq-options',[0.025]))
        scale_2_freq = r.choice_from(setup_key,config.get('scale-2-freq-options',[0.02]))
        scale_1_scale = r.choice_from(setup_key,config.get('scale-1-scale-options',[0.02]))
        scale_2_scale = r.choice_from(setup_key,config.get('scale-2-scale-options',[0.02]))
        num_sin_coeffs = r.choice_from(setup_key,config.get('num-sin-coefficients-options',[18]))

        f1_scale = r.choice_from(setup_key,config.get('f1-scale-options',[0.2]))
        f2_scale = r.choice_from(setup_key,config.get('f2-scale-options',[0.4]))
        f3_scale = r.choice_from(setup_key,config.get('f3-scale-options',[0.15]))


        for current_row in range(HEIGHT):

            loop_key = r.reset_key(loop_key)

            # self_length = SEGMENT_LENGTH+int(10*np.sin(np.pi*i*0.01))
            self_length = SEGMENT_LENGTH
            # scale_1 = 0.1 * (1 - interpolation_h_1[current_row]) + 0.15 * interpolation_h_1[current_row]
            scale_1 = 0.1 + scale_1_scale * np.sin(np.pi * current_row * scale_1_freq )
            scale_2 = 0.1 + scale_2_scale * np.sin(np.pi * current_row * scale_2_freq )
            p1 = m.MarkovModel(
                values=[scale_1, -scale_2],
                preference_matrix=data.str2mat('1 5, 5 1'),
                self_length=self_length,
                parent_rkey=r.bind_generator_from(loop_key)

            )

            p2 = m.MarkovModel(
                values=[-scale_1, scale_2],
                preference_matrix=data.str2mat('1 5, 5 1'),
                self_length=self_length,
                parent_rkey=r.bind_generator_from(loop_key)

            )

            zeros = m.MarkovModel(
                values=[0,0],
                preference_matrix=data.str2mat('1 1, 1 1'),
                self_length=self_length*3,
                parent_rkey=r.bind_generator_from(loop_key)

            )

            jumps = m.MarkovModel(
                values=[-0.5, 0.5],
                preference_matrix=data.str2mat('1 1, 1 1'),
                self_length=1,
                parent_rkey=r.bind_generator_from(loop_key)

            )

            num_coefs = num_sin_coeffs
            vs = np.sin(np.linspace(0, 1, num_coefs) * np.pi * 2)*0.1
            p3 = m.SimpleProgression(
                values=vs,
                start_probs=0,
                self_length=[num_coefs],
                parent_rkey=r.bind_generator_from(loop_key)
            )

            p = m.MarkovModel(
                values=[p1, p2, p3, jumps, zeros],
                start_probs=2,
                preference_matrix=data.str2mat(
                    '0 1 2 2 1, 1 0 2 2 1, 1 1 4 2 2, 1 1 2 0 0, 1 1 1 1 2'),
                self_length=WIDTH//SEGMENT_LENGTH+1,
                parent_rkey=r.bind_generator_from(loop_key)
            )

            num_samples_1 = WIDTH//4
            num_samples_2 = WIDTH//3
            sample_x_up = m.sample_markov_hierarchy(p, num_samples_1)
            sample_x_down = m.sample_markov_hierarchy(p, num_samples_2)

            sample_x_up_int = data.integrate_series(sample_x_up,2,mean_influence=1)
            sample_x_down_int = data.integrate_series(sample_x_down,2,mean_influence=1)

            f1 = 0.5 + f1_scale * np.sin(np.pi * current_row * 0.002 )
            f2 = -1 - f2_scale * np.sin(np.pi * current_row * 0.002 )
            f3 = 0.3 + f3_scale * np.sin(np.pi * current_row * 0.001 )

            sample_x_up_int = data.concat_signals(
                [sample_x_up_int]*4,
                [f1,f2,f1,f2])

            sample_x_down_int = data.concat_signals(
                [sample_x_down_int,sample_x_down_int,sample_x_down_int],
                [f3, f1, f3])
            sample_x_down_int = np.r_[sample_x_down_int[0],sample_x_down_int]


            # roll_distance = 500 + int((interpolation_h_2[current_row]-0.5)*250)
            # roll_distance = 500 + int(current_row)
            # print(roll_distance)
            # sample_x_down_int = np.roll(sample_x_down_int, roll_distance)


            sample_x = sample_x_up_int + sample_x_down_int
            interpolation_sequence = sample_x[:HEIGHT]


            interpolation_sequence = gaussian_filter(interpolation_sequence,sigma=1)
            interpolation_sequence -= np.min(interpolation_sequence)
            interpolation_sequence /= np.max(interpolation_sequence)
            # interpolation_sequence = data.ease_inout_sin(interpolation_sequence)
            interpolation_sequence *= 3

            # interpolation_sequence *= 2
            # print(interpolation_sequence)

            gradient = data.interpolate(
                color_lines[:,current_row,:],
                interpolation_sequence,
                value_influences=meta
            )
            gradient = color.cam02_2_srgb(gradient)
            image[current_row] = gradient

            plots += [np.copy(interpolation_sequence)]

        image = data.upscale_nearest(image,ny=UPSCALE_FACTOR_Y,nx=UPSCALE_FACTOR_X)
        image[image<0] = 0
        image[image>255] = 255

        if SHOW_DEBUG_DATA is True:
            viz.animate_plots_y(plots)

        return image
Exemple #5
0
                       self_length=self_length_1)

    p3 = m.MarkovModel(values=[0.01, 0.01],
                       preference_matrix=data.str2mat('1 1, 1 1'),
                       self_length=self_length_1)

    p4 = m.MarkovModel(values=[0.02, 0.02],
                       preference_matrix=data.str2mat('1 1, 1 1'),
                       self_length=self_length_1)

    p5 = m.SimpleProgression(
        values=[-0.02, -0.01, 0, 0.01, 0.02, 0.02, 0.01, 0, -0.01, -0.02],
        self_length=self_length_2)

    p = m.MarkovModel(
        values=[p1, p2, p3, p4, p5],
        preference_matrix=data.str2mat(
            ' 1 0 1 0 1, 0 0 0 0 1, 1 0 1 0 1, 0 0 0 0 1 ,1 1 1 1 1 '),
    )

    num_samples = 1000
    sample_x = m.sample_markov_hierarchy(p, num_samples)
    sample_x_1 = np.cumsum(sample_x)
    sample_x_2 = np.cumsum(sample_x_1)
    sample_x_2 /= np.max(np.abs(sample_x_2))

    plt.plot(sample_x_2 + i * 0.05, label=str(i), c='b', alpha=0.1)

# plt.legend()
plt.show()
def generate_patch(height, width, color_dict, rkey):

    patch = np.zeros((height, width, 3), dtype='float64')

    color_start_lengths = np.array(
        [int(i) for i in color.get_meta_from_palette(color_dict)])

    num_color_samples = width // np.min(color_start_lengths) + 20

    color_codes = color.get_keys_from_palette(color_dict)
    pattern = m.FuzzyProgression(values=color_codes,
                                 positive_shifts=3,
                                 negative_shifts=3,
                                 self_length=num_color_samples,
                                 parent_rkey=r.bind_generator_from(rkey))

    sample_raw_start = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    sample_raw_down_start = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    # print(sample_raw_start)
    sample_raw_end = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    sample_raw_down_end = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    sample_raw_backup = m.sample_markov_hierarchy(
        pattern, num_color_samples).astype('int32')
    # making the probability of same color used smaller
    replace_mask = sample_raw_start == sample_raw_end
    sample_raw_end[replace_mask] = sample_raw_backup[replace_mask]

    sample_start = color.replace_indices_with_colors(sample_raw_start,
                                                     color_dict)
    sample_end = color.replace_indices_with_colors(sample_raw_end, color_dict)

    sample_down_start = color.replace_indices_with_colors(
        sample_raw_down_start, color_dict)
    sample_down_end = color.replace_indices_with_colors(
        sample_raw_down_end, color_dict)

    switch_key = r.bind_generator_from(rkey)
    switch = np.array([
        r.choice_from(switch_key, [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.get_meta_for_each_sample(sample_raw_start,
                                                   color_dict)

    start_lengths = np.array([int(i) for i in start_lengths])
    start_lengths = np.cumsum(start_lengths)

    num_vertical_reps = 2
    num_vertical_samples = height // num_vertical_reps + 3
    model = m.MarkovModel(
        values=np.arange(0, 41, 10) - 20,
        preference_matrix=data.str2mat(
            '0 1 5 1 0, 1 2 5 1 0, 0 1 10 1 0, 0 1 5 2 1, 0 1 5 1 0'),
        self_length=num_vertical_samples,
        parent_rkey=r.bind_generator_from(rkey))

    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_from(
                            rkey, [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

    transition = np.linspace(0, 1, num_vertical_samples)
    sample_start_gradient = sample_start[:, :, None] * (
        1 - transition) + sample_down_start[:, :, None] * transition
    sample_end_gradient = sample_end[:, :, None] * (
        1 - transition) + sample_down_end[:, :, None] * transition

    multiples_choices = r.choice_from(rkey,
                                      config.get('multiples-choices',
                                                 [20, 30, 40, 50]),
                                      size=(6, ))
    # print('multiples-choices',multiples_choices)

    while i < height:
        loop_key = r.bind_generator_from(rkey)
        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:]]
        #
        # samples_start_masked = sample_start_gradient[:,:,i//num_vertical_reps][mask[1:]]
        # samples_end_masked = sample_end_gradient[:,:,i//num_vertical_reps][mask[1:]]

        p_switch = config.get('gradient-switch-p', 0.5)

        switch = r.choice_from(loop_key, [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_from(loop_key, multiples_choices)

        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