def grime(tensor, shape): """ """ value_shape = [shape[0], shape[1], 1] mask = multires(5, value_shape, distrib="exp", octaves=8, refract_range=1.0, deriv=3, deriv_alpha=.5) dusty = effects.blend(tensor, .25, tf.square(mask) * .125) specks = basic( [int(shape[0] * .25), int(shape[1] * .25)], value_shape, distrib="exp", refract_range=.1) specks = 1.0 - tf.sqrt(effects.normalize(tf.maximum(specks - .5, 0.0))) dusty = effects.blend(dusty, basic([shape[0], shape[1]], value_shape), .125) * specks return effects.blend(tensor, dusty, mask)
def glitch(tensor, shape, time=0.0, speed=1.0): """ 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, time=time, speed=speed, distrib=ValueDistribution.simplex, 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.color_map(base, stylized, shape, horizontal=True, displacement=2.5) if channels in (1, 3): jpegged = effects.jpeg_decimate(jpegged, 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()) 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)) combined = effects.blend(combined, effects.pixel_sort(combined, shape), 1.0 - base) combined = tf.image.adjust_contrast(combined, 1.75) return combined
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 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 clouds(input_filename): tensor = tf.image.convert_image_dtype(load(input_filename), tf.float32) pre_shape = [SMALL_Y, SMALL_X, 1] post_shape = [LARGE_Y, LARGE_X, 1] control_kwargs = { "freq": FREQ * 2, "lattice_drift": 1, "octaves": OCTAVES, "ridges": True, "shape": pre_shape, "warp_freq": 3, "warp_range": .25, "warp_octaves": 2, } control = generators.multires(**control_kwargs) layer_0 = tf.ones(pre_shape) layer_1 = tf.zeros(pre_shape) combined = effects.blend_layers(control, pre_shape, 1.0, layer_0, layer_1) shadow = effects.offset(combined, pre_shape, random.randint(-15, 15), random.randint(-15, 15)) shadow = tf.minimum(shadow * 2.5, 1.0) shadow = effects.convolve(effects.ConvKernel.blur, shadow, pre_shape) shadow = effects.convolve(effects.ConvKernel.blur, shadow, pre_shape) shadow = effects.convolve(effects.ConvKernel.blur, shadow, pre_shape) shadow = effects.resample(shadow, post_shape) combined = effects.resample(combined, post_shape) tensor = effects.blend(tensor, tf.zeros(post_shape), shadow * .5) tensor = effects.blend(tensor, tf.ones(post_shape), combined) post_shape = [LARGE_Y, LARGE_X, 3] tensor = effects.shadow(tensor, post_shape, alpha=.25) tensor = effects.bloom(tensor, post_shape, .333) tensor = recipes.dither(tensor, post_shape, .075) combined = tf.image.adjust_contrast(combined, 1.125) with tf.Session().as_default(): save(tensor, FINAL_FILENAME)
def fibers(tensor, shape, time=0.0, speed=1.0): """ """ value_shape = [shape[0], shape[1], 1] for i in range(4): mask = basic( 4, value_shape, with_worms=4, worms_alpha=1, worms_density=.05 + random.random() * .00125, worms_duration=1, worms_kink=random.randint(5, 10), worms_stride=.75, worms_stride_deviation=.125, time=time, speed=speed, distrib=ValueDistribution.simplex, ) brightness = basic(128, shape, time=time, speed=speed, distrib=ValueDistribution.simplex, saturation=2.0) tensor = effects.blend(tensor, brightness, mask * .5) return tensor
def stray_hair(tensor, shape, time=0.0, speed=1.0): """ """ value_shape = [shape[0], shape[1], 1] mask = basic( 4, value_shape, with_worms=4, worms_alpha=1, worms_density=.0025 + random.random() * .00125, worms_duration=random.randint(8, 16), worms_kink=random.randint(5, 50), worms_stride=.5, worms_stride_deviation=.25, time=time, speed=speed, distrib=ValueDistribution.simplex, ) brightness = basic(32, value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex) return effects.blend(tensor, brightness * .333, mask * .666)
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 dither(tensor, shape, amount): """ """ height, width, channels = shape white_noise = basic([height, width], [height, width, 1]) return effects.blend(tensor, white_noise, amount)
def clouds(tensor, shape, time=0.0, speed=1.0): """Top-down cloud cover effect""" pre_shape = [int(shape[0] * .25) or 1, int(shape[1] * .25) or 1, 1] control_kwargs = { "freq": random.randint(2, 4), "lattice_drift": 1, "octaves": 8, "ridges": True, "speed": speed, "shape": pre_shape, "time": time, "warp_freq": 3, "warp_range": .125, "warp_octaves": 2, "distrib": ValueDistribution.simplex, } control = multires(**control_kwargs) layer_0 = tf.ones(pre_shape) layer_1 = tf.zeros(pre_shape) combined = effects.blend_layers(control, pre_shape, 1.0, layer_0, layer_1) shadow = effects.offset(combined, pre_shape, random.randint(-15, 15), random.randint(-15, 15)) shadow = tf.minimum(shadow * 2.5, 1.0) for _ in range(3): shadow = effects.convolve(ValueMask.conv2d_blur, shadow, pre_shape) post_shape = [shape[0], shape[1], 1] shadow = effects.resample(shadow, post_shape) combined = effects.resample(combined, post_shape) tensor = effects.blend(tensor, tf.zeros(shape), shadow * .75) tensor = effects.blend(tensor, tf.ones(shape), combined) tensor = effects.shadow(tensor, shape, alpha=.5) return tensor
def dither(tensor, shape, amount, time=0.0, speed=1.0): """ """ height, width, channels = shape white_noise = basic([height, width], [height, width, 1], time=time, speed=speed, distrib=ValueDistribution.simplex) return effects.blend(tensor, white_noise, amount)
def snow(tensor, shape, amount): """ """ height, width, channels = shape white_noise_1 = basic([height, width], [height, width, 1], wavelet=True, refract_range=10) white_noise_2 = tf.maximum( basic([int(height * .75), int(width * .75)], [height, width, 1]) - (1 - amount), 0) * 2 return effects.blend(tensor, white_noise_1, white_noise_2)
def grime(tensor, shape, time=0.0, speed=1.0): """ """ value_shape = [shape[0], shape[1], 1] mask = multires(5, value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex_exp, octaves=8, refract_range=1.0, refract_y_from_offset=True, deriv=3, deriv_alpha=.5) dusty = effects.blend(tensor, .25, tf.square(mask) * .125) specks = basic( [int(shape[0] * .25), int(shape[1] * .25)], value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex_exp, refract_range=.1) specks = 1.0 - tf.sqrt(effects.normalize(tf.maximum(specks - .5, 0.0))) dusty = effects.blend( dusty, basic([shape[0], shape[1]], value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex), .125) * specks return effects.blend(tensor, dusty, mask)
def frame(tensor, shape, time=0.0, speed=1.0): """ """ half_shape = [int(shape[0] * .5), int(shape[1] * .5), shape[2]] half_value_shape = [half_shape[0], half_shape[1], 1] noise = multires(64, half_value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex, octaves=8) black = tf.zeros(half_value_shape) white = tf.ones(half_value_shape) mask = effects.singularity(None, half_value_shape, 1, dist_func=3, inverse=True) mask = effects.normalize(mask + noise * .005) mask = effects.blend_layers(tf.sqrt(mask), half_value_shape, 0.0125, white, black, black, black) faded = effects._downsample(tensor, shape, half_shape) faded = tf.image.adjust_brightness(faded, .1) faded = tf.image.adjust_contrast(faded, .75) faded = effects.light_leak(faded, half_shape, .125) faded = effects.vignette(faded, half_shape, 0.05, .75) edge_texture = white * .9 + effects.shadow(noise, half_value_shape, 1.0) * .1 out = effects.blend(faded, edge_texture, mask) out = effects.aberration(out, half_shape, .00666) out = grime(out, half_shape) out = tf.image.adjust_saturation(out, .5) out = tf.image.random_hue(out, .05) out = effects.resample(out, shape) out = scratches(out, shape) out = stray_hair(out, shape) return out
def snow(tensor, shape, amount, time=0.0, speed=1.0): """ """ height, width, channels = shape static = basic([height, width], [height, width, 1], time=time, speed=speed * 100, distrib=ValueDistribution.simplex, spline_order=0) static_limiter = basic([height, width], [height, width, 1], time=time, speed=speed * 100, distrib=ValueDistribution.simplex_exp, spline_order=0) * amount return effects.blend(tensor, static, static_limiter)
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 stray_hair(tensor, shape): """ """ value_shape = [shape[0], shape[1], 1] mask = basic( 4, value_shape, with_worms=4, worms_alpha=1, worms_density=.0025 + random.random() * .00125, worms_duration=random.randint(8, 16), worms_kink=random.randint(5, 50), worms_stride=.5, worms_stride_deviation=.25, ) brightness = basic(32, value_shape) return effects.blend(tensor, brightness * .333, mask * .666)
def crt(tensor, shape, time=0.0, speed=1.0): """ Apply vintage CRT snow and scanlines. :param Tensor tensor: :param list[int] shape: """ height, width, channels = shape value_shape = [height, width, 1] # Horizontal scanlines scan_noise = tf.tile( basic([2, 1], [2, 1, 1], time=time, speed=speed, distrib=ValueDistribution.simplex, spline_order=0), [int(height * .125) or 1, width, 1]) scan_noise = effects.resample(scan_noise, value_shape) scan_noise = lens_warp(scan_noise, value_shape, time=time, speed=speed) tensor = effects.normalize( effects.blend(tensor, (tensor + scan_noise) * scan_noise, 0.05)) if channels == 3: tensor = effects.aberration(tensor, shape, .0075 + random.random() * .0075) tensor = tf.image.random_hue(tensor, .125) tensor = tf.image.adjust_saturation(tensor, 1.25) tensor = tf.image.adjust_contrast(tensor, 1.25) tensor = effects.vignette(tensor, shape, brightness=0, alpha=random.random() * .175) return tensor
def vhs(tensor, shape, time=0.0, speed=1.0): """ Apply a bad VHS tracking effect. :param Tensor tensor: :param list[int] shape: :return: Tensor """ height, width, channels = shape # Generate scan noise scan_noise = basic( [int(height * .5) + 1, int(width * .05) + 1], [height, width, 1], time=time, speed=speed, spline_order=1, distrib=ValueDistribution.simplex) # Create horizontal offsets grad = basic([int(random.random() * 10) + 5, 1], [height, width, 1], time=time, speed=speed, distrib=ValueDistribution.simplex) grad = tf.maximum(grad - .5, 0) grad = tf.minimum(grad * 2, 1) x_index = effects.row_index(shape) x_index -= tf.squeeze( tf.cast(scan_noise * width * tf.square(grad), tf.int32)) x_index = x_index % width tensor = effects.blend(tensor, scan_noise, grad) identity = tf.stack([effects.column_index(shape), x_index], 2) tensor = tf.gather_nd(tensor, identity) return tensor
def watermark(tensor, shape, time=0.0, speed=1.0): """ """ value_shape = [int(shape[0] * .5), int(shape[1] * .5), 1] value_shape = [shape[0], shape[1], 1] mask = basic(240, value_shape, spline_order=0, distrib=ValueDistribution.ones, mask="numeric") mask = crt(mask, value_shape) mask = effects.warp(mask, value_shape, [2, 4], octaves=1, displacement=.5, time=time, speed=speed) mask *= tf.square( basic(2, value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex)) value_shape = [shape[0], shape[1], 1] brightness = basic(16, value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex) return effects.blend(tensor, brightness, mask * .125)
def on_screen_display(tensor, shape): glyph_count = random.randint(3, 6) _masks = [ ValueMask.bank_ocr, ValueMask.hex, ValueMask.numeric, ] mask = _masks[random.randint(0, len(_masks) - 1)] mask_shape = masks.mask_shape(mask) width = int(shape[1] / 24) width = mask_shape[1] * int( width / mask_shape[1]) # Make sure the mask divides evenly height = mask_shape[0] * int(width / mask_shape[1]) width *= glyph_count freq = [mask_shape[0], mask_shape[1] * glyph_count] this_mask = basic(freq, [height, width, shape[2]], corners=True, spline_order=0, distrib=ValueDistribution.ones, mask=mask) rendered_mask = tf.pad( this_mask, tf.stack([[25, shape[0] - height - 25], [shape[1] - width - 25, 25], [0, 0]])) alpha = .5 + random.random() * .25 return effects.blend(tensor, tf.maximum(rendered_mask, tensor), alpha)
def spooky_ticker(tensor, shape): """ """ if random.random() > .75: tensor = on_screen_display(tensor, shape) _masks = [ ValueMask.arecibo_nucleotide, ValueMask.arecibo_num, ValueMask.bank_ocr, ValueMask.bar_code, ValueMask.bar_code_short, ValueMask.emoji, ValueMask.fat_lcd_hex, ValueMask.hex, ValueMask.iching, ValueMask.ideogram, ValueMask.invaders, ValueMask.lcd, ValueMask.letters, ValueMask.matrix, ValueMask.numeric, ValueMask.script, ValueMask.white_bear, ] bottom_padding = 2 rendered_mask = tf.zeros(shape) for _ in range(random.randint(1, 3)): mask = _masks[random.randint(0, len(_masks) - 1)] mask_shape = masks.mask_shape(mask) multiplier = 1 if mask != ValueMask.script and ( mask_shape[1] == 1 or mask_shape[1] >= 10) else 2 width = int(shape[1] / multiplier) or 1 width = mask_shape[1] * int( width / mask_shape[1]) # Make sure the mask divides evenly into width freq = [mask_shape[0], width] this_mask = basic(freq, [mask_shape[0], width, 1], corners=True, spline_order=0, distrib=ValueDistribution.ones, mask=mask) this_mask = effects.resample(this_mask, [mask_shape[0] * multiplier, shape[1]], spline_order=1) rendered_mask += tf.pad( this_mask, tf.stack([[ shape[0] - mask_shape[0] * multiplier - bottom_padding, bottom_padding ], [0, 0], [0, 0]])) bottom_padding += mask_shape[0] * multiplier + 2 alpha = .5 + random.random() * .25 # shadow tensor = effects.blend( tensor, tensor * 1.0 - effects.offset(rendered_mask, shape, -1, -1), alpha * .333) return effects.blend(tensor, tf.maximum(rendered_mask, tensor), alpha)
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)
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')