def scanline_error(tensor, shape): """ """ height, width, channels = shape value_shape = [height, width, 1] error_line = tf.maximum( basic([int(height * .75), 1], value_shape, distrib=ValueDistribution.exp) - .5, 0) error_swerve = tf.maximum( basic([int(height * .01), 1], value_shape, distrib=ValueDistribution.exp) - .5, 0) error_line *= error_swerve error_swerve *= 2 white_noise = basic([int(height * .75), 1], value_shape) white_noise = effects.blend(0, white_noise, error_swerve) error = error_line + white_noise y_index = effects.column_index(shape) x_index = (effects.row_index(shape) - tf.cast( effects.value_map(error, value_shape) * width * .025, tf.int32)) % width return tf.minimum( tf.gather_nd(tensor, tf.stack([y_index, x_index], 2)) + error_line * white_noise * 4, 1)
def blended(): shape = [LARGE_Y, LARGE_X, 3] erode_kwargs = { "alpha": .025, "density": 250, "iterations": 50, "inverse": True, } control = tf.image.convert_image_dtype(load(CONTROL_FILENAME), tf.float32) water = tf.ones(shape) * tf.stack([.05, .2, .333]) water = effects.blend(water, control * 4.0, .125) low = tf.image.convert_image_dtype(load(LOW_FILENAME), tf.float32) mid = tf.image.convert_image_dtype(load(MID_FILENAME), tf.float32) high = tf.image.convert_image_dtype(load(HIGH_FILENAME), tf.float32) blend_control = generators.multires(shape=shape, freq=FREQ * 4, ridges=True, octaves=4) blend_control = 1.0 - effects.value_map( blend_control, shape, keep_dims=True) * .5 combined_land = effects.blend_layers(control, shape, blend_control, control * 2, low, mid, high) combined_land = effects.erode(combined_land, shape, xy_blend=.25, **erode_kwargs) combined_land = effects.erode(combined_land, shape, **erode_kwargs) combined_land_0 = effects.shadow(combined_land, shape, alpha=1.0) combined_land_1 = effects.shadow(combined_land, shape, alpha=1.0, reference=control) combined_land = effects.blend(combined_land_0, combined_land_1, .5) combined = effects.blend_layers(control, shape, .01, water, combined_land, combined_land, combined_land) combined = effects.blend(combined_land, combined, .625) combined = tf.image.adjust_brightness(combined, .1) combined = tf.image.adjust_contrast(combined, .75) combined = tf.image.adjust_saturation(combined, .625) with tf.Session().as_default(): save(combined, BLENDED_FILENAME)
def glitch(tensor, shape): """ Apply a glitch effect. :param Tensor tensor: :param list[int] shape: :return: Tensor """ height, width, channels = shape tensor = effects.normalize(tensor) base = multires(2, shape, octaves=random.randint(2, 5), spline_order=0, refract_range=random.random()) stylized = effects.normalize( effects.color_map(base, tensor, shape, horizontal=True, displacement=2.5)) jpegged = effects.jpeg_decimate( effects.color_map(base, stylized, shape, horizontal=True, displacement=2.5), shape) # Offset a single color channel separated = [stylized[:, :, i] for i in range(channels)] x_index = (effects.row_index(shape) + random.randint(1, width)) % width index = tf.cast(tf.stack([effects.column_index(shape), x_index], 2), tf.int32) channel = random.randint(0, channels - 1) separated[channel] = effects.normalize( tf.gather_nd(separated[channel], index) % random.random()) channel = random.randint(0, channels - 1) top, _ = tf.nn.top_k(effects.value_map(tensor, shape), k=width) separated[channel] += top stylized = tf.stack(separated, 2) combined = effects.blend(tf.multiply(stylized, 1.0), jpegged, base) combined = effects.blend(tensor, combined, tf.maximum(base * 2 - 1, 0)) return combined
def basic(ctx, width, height, input_dir, name): shape = [height, width, 3] filenames = [ f for f in os.listdir(input_dir) if f.endswith(".png") or f.endswith(".jpg") ] collage_count = min(random.randint(3, 5), len(filenames)) collage_images = [] for i in range(collage_count + 1): index = random.randint(0, len(filenames) - 1) collage_input = tf.image.convert_image_dtype(util.load( os.path.join(input_dir, filenames[index])), dtype=tf.float32) collage_images.append(effects.resample(collage_input, shape)) base = generators.basic(freq=random.randint(2, 5), shape=shape, lattice_drift=random.randint(0, 1), hue_range=random.random()) control = effects.value_map(collage_images.pop(), shape, keep_dims=True) 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) tensor = tf.image.adjust_brightness(tensor, .05) tensor = tf.image.adjust_contrast(tensor, 1.25) with tf.Session().as_default(): save(tensor, name) print(name)
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')
def frame(ctx, input_dir, frame, seed, name): generators.set_seed(seed) shape = [512, 512, 3] dirnames = [ d for d in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, d)) ] if not dirnames: click.echo("Couldn't determine directory names inside of input dir " + input_dir) sys.exit(1) collage_count = min(random.randint(4, 6), len(dirnames)) collage_images = [] for i in range(collage_count + 1): index = random.randint(0, len(dirnames) - 1) dirname = dirnames[index] filenames = [ f for f in sorted( os.listdir(os.path.join(input_dir, dirnames[index]))) if f.endswith('.png') ] if not filenames: continue input_filename = os.path.join(input_dir, dirnames[index], filenames[frame]) collage_images.append( tf.image.convert_image_dtype(util.load(input_filename, channels=3), dtype=tf.float32)) base = generators.basic(freq=random.randint(2, 4), shape=shape, hue_range=random.random(), time=frame / 30.0, speed=0.125, distrib="simplex") control = effects.value_map(collage_images.pop(), shape, keep_dims=True) control = effects.convolve(effects.ValueMask.conv2d_blur, control, [512, 512, 1]) with tf.Session().as_default(): 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) util.save(tensor, name)