コード例 #1
0
ファイル: noisemaker.py プロジェクト: Kulinark/py-noisemaker
def effect(ctx, seed, filename, no_resize, time, speed, preset_name, input_filename):
    if not seed:
        seed = random.randint(1, MAX_SEED_VALUE)

    value.set_seed(seed)
    reload_presets(PRESETS)

    input_shape = util.shape_from_file(input_filename)

    input_shape[2] = min(input_shape[2], 3)

    tensor = tf.image.convert_image_dtype(util.load(input_filename, channels=input_shape[2]), dtype=tf.float32)

    if preset_name == "random":
        preset_name = list(EFFECT_PRESETS)[random.randint(0, len(EFFECT_PRESETS) - 1)]

    print(f"{preset_name} (seed: {seed})")

    preset = EFFECT_PRESETS[preset_name]

    if no_resize:
        shape = input_shape

    else:
        shape = [1024, 1024, input_shape[2]]

        tensor = effects.square_crop_and_resize(tensor, input_shape, shape[0])

    try:
        preset.render(tensor=tensor, shape=shape, time=time, speed=speed, filename=filename)

    except Exception as e:
        util.logger.error(f"preset.render() failed: {e}\nSeed: {seed}\nArgs: {preset.__dict__}")
        raise
コード例 #2
0
ファイル: crop.py プロジェクト: aayars/py-noisemaker
def main(ctx, name, retro_upscale, input_filename):
    shape = shape_from_file(input_filename)

    tensor = tf.image.convert_image_dtype(load(input_filename, channels=3),
                                          tf.float32)

    if retro_upscale:
        shape = [shape[0] * 2, shape[1] * 2, shape[2]]

        tensor = value.resample(tensor, shape, spline_order=0)

    tensor = effects.square_crop_and_resize(tensor, shape, 1024)

    with tf.compat.v1.Session().as_default():
        save(tensor, name)
コード例 #3
0
def main(ctx, seed, name, no_resize, overrides, time, preset_name, input_filename):
    presets.bake_presets(seed)

    input_shape = effects.shape_from_file(input_filename)

    input_shape[2] = min(input_shape[2], 3)

    tensor = tf.image.convert_image_dtype(load(input_filename, channels=input_shape[2]), dtype=tf.float32)

    if preset_name == 'random':
        preset_name = 'random-effect'

    kwargs = presets.preset(preset_name)

    print(kwargs['name'])

    kwargs['time'] = time

    if 'freq' not in kwargs:
        kwargs['freq'] = [3, 3]

    if 'octaves' not in kwargs:
        kwargs['octaves'] = 1

    if 'ridges' not in kwargs:
        kwargs['ridges'] = False

    if no_resize:
        kwargs['shape'] = input_shape

    else:
        kwargs['shape'] = [1024, 1024, input_shape[2]]

        tensor = effects.square_crop_and_resize(tensor, input_shape, kwargs['shape'][0])

    if overrides:
        kwargs.update(json.loads(overrides))

    tensor = effects.post_process(tensor, **kwargs)
    tensor = recipes.post_process(tensor, **kwargs)

    with tf.Session().as_default():
        save(tensor, name)
コード例 #4
0
ファイル: collagemaker.py プロジェクト: raboof/py-noisemaker
def basic(ctx, width, height, input_dir, name, control_filename,
          retro_upscale):
    shape = [height, width,
             3]  # Any shape you want, as long as it's [1024, 1024, 3]

    filenames = []

    for root, _, files in os.walk(input_dir):
        for filename in files:
            if filename.endswith(('.png', '.jpg')):
                filenames.append(os.path.join(root, filename))

    collage_count = min(random.randint(4, 6), len(filenames))
    collage_images = []

    for i in range(collage_count + 1):
        index = random.randint(0, len(filenames) - 1)

        input_filename = os.path.join(input_dir, filenames[index])

        collage_input = tf.image.convert_image_dtype(util.load(input_filename,
                                                               channels=3),
                                                     dtype=tf.float32)

        input_shape = effects.shape_from_file(input_filename)

        if retro_upscale:
            input_shape = [
                input_shape[0] * 2, input_shape[1] * 2, input_shape[2]
            ]

            collage_input = effects.resample(collage_input,
                                             input_shape,
                                             spline_order=0)

        collage_input = effects.square_crop_and_resize(collage_input,
                                                       input_shape, 1024)

        collage_images.append(collage_input)

    base = generators.basic(freq=random.randint(2, 5),
                            shape=shape,
                            lattice_drift=random.randint(0, 1),
                            hue_range=random.random())

    if control_filename:
        control = tf.image.convert_image_dtype(util.load(control_filename,
                                                         channels=1),
                                               dtype=tf.float32)

        control = effects.square_crop_and_resize(
            control, effects.shape_from_file(control_filename), 1024)

        control = effects.value_map(control, shape, keep_dims=True)

    else:
        control = effects.value_map(collage_images.pop(),
                                    shape,
                                    keep_dims=True)

    control = effects.convolve(effects.ValueMask.conv2d_blur, control,
                               [height, width, 1])

    with tf.Session().as_default():
        # sort collage images by brightness
        collage_images = [
            j[1] for j in sorted([(tf.reduce_sum(i).eval(), i)
                                  for i in collage_images])
        ]

        tensor = effects.blend_layers(control, shape,
                                      random.random() * .5, *collage_images)

        tensor = effects.blend(tensor, base, .125 + random.random() * .125)

        tensor = effects.bloom(tensor,
                               shape,
                               alpha=.25 + random.random() * .125)
        tensor = effects.shadow(tensor,
                                shape,
                                alpha=.25 + random.random() * .125,
                                reference=control)

        tensor = tf.image.adjust_brightness(tensor, .05)
        tensor = tf.image.adjust_contrast(tensor, 1.25)

        save(tensor, name)

    print('mashup')