def scratches(tensor, shape, time=0.0, speed=1.0): """ """ value_shape = [shape[0], shape[1], 1] for i in range(4): mask = basic( random.randint(2, 4), value_shape, with_worms=[1, 3][random.randint(0, 1)], worms_alpha=1, worms_density=.25 + random.random() * .25, worms_duration=2 + random.random() * 2, worms_kink=.125 + random.random() * .125, worms_stride=.75, worms_stride_deviation=.5, time=time, speed=speed, distrib=ValueDistribution.simplex, ) mask -= basic(random.randint(2, 4), value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex) * 2.0 mask = tf.maximum(mask, 0.0) tensor = tf.maximum(tensor, mask * 8.0) tensor = tf.minimum(tensor, 1.0) return tensor
def interference(tensor, shape, time=0.0, speed=1.0): """ """ height, width, channels = shape value_shape = [height, width, 1] distortion = basic(2, value_shape, time=time, speed=speed, distribution=ValueDistribution.simplex, corners=True) scan_noise = basic([2, 1], [2, 1, 1], time=time, speed=speed, distribution=ValueDistribution.simplex) scan_noise = tf.tile(scan_noise, [random.randint(32, 128), width, 1]) scan_noise = effects.resample(scan_noise, value_shape, spline_order=0) scan_noise = effects.refract(scan_noise, value_shape, 1, reference_x=distortion) tensor = 1.0 - (1.0 - tensor) * scan_noise return tensor
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 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 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 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 crt(tensor, shape): """ Apply vintage CRT snow and scanlines. :param Tensor tensor: :param list[int] shape: """ height, width, channels = shape value_shape = [height, width, 1] distortion = basic(3, value_shape) distortion_amount = .25 white_noise = basic(int(height * .75), value_shape, spline_order=0) - .5 white_noise = effects.center_mask( white_noise, effects.refract(white_noise, value_shape, distortion_amount, reference_x=distortion), value_shape) white_noise2 = basic([int(height * .5), int(width * .25)], value_shape) white_noise2 = effects.center_mask( white_noise2, effects.refract(white_noise2, value_shape, distortion_amount, reference_x=distortion), value_shape) tensor = effects.blend_cosine(tensor, white_noise, white_noise2 * .25) scan_noise = tf.tile(basic([2, 1], [2, 1, 1]), [int(height * .333), width, 1]) scan_noise = effects.resample(scan_noise, value_shape) scan_noise = effects.center_mask( scan_noise, effects.refract(scan_noise, value_shape, distortion_amount, reference_x=distortion), value_shape) tensor = effects.blend_cosine(tensor, scan_noise, 0.25) if channels <= 2: return tensor tensor = tf.image.random_hue(tensor, .125) tensor = tf.image.adjust_saturation(tensor, 1.25) return tensor
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 lens_warp(tensor, shape, displacement=.0625, time=0.0, speed=1.0): """ """ value_shape = [shape[0], shape[1], 1] # Fake CRT lens shape mask = tf.pow(effects.singularity(None, value_shape), 5) # obscure center pinch # Displacement values multiplied by mask to make it wavy towards the edges distortion_x = (basic( 2, value_shape, time=time, speed=speed, distrib=ValueDistribution.simplex, spline_order=2, lattice_drift=1.0, ) * 2.0 - 1.0) * mask return effects.refract(tensor, shape, displacement, reference_x=distortion_x)
def render(ctx, width, height, input_dir, voronoi_func, voronoi_nth, point_freq, point_distrib, point_drift, name): shape = [height, width, 3] x, y = points.point_cloud(point_freq, distrib=point_distrib, shape=shape, drift=point_drift) base = generators.basic(freq=random.randint(2, 4), shape=shape, lattice_drift=random.randint(0, 1), hue_range=random.random()) tensor = effects.voronoi(base, shape, diagram_type=effects.VoronoiDiagramType.collage, xy=(x, y, len(x)), nth=voronoi_nth, input_dir=input_dir, alpha=.333 + random.random() * .333) tensor = effects.bloom(tensor, shape, alpha=.333 + random.random() * .333) with tf.Session().as_default(): save(tensor, name) print('mashup')
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 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 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 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 mashup(ctx, input_dir, filename, control_filename, time, speed, seed): filenames = [] for root, _, files in os.walk(input_dir): for f in files: if f.endswith(('.png', '.jpg')): filenames.append(os.path.join(root, f)) 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) collage_images.append(collage_input) if control_filename: control_shape = util.shape_from_file(control_filename) control = tf.image.convert_image_dtype(util.load(control_filename, channels=control_shape[2]), dtype=tf.float32) else: control = collage_images.pop() shape = tf.shape(control) # All images need to be the same size! control = value.value_map(control, shape, keepdims=True) base = generators.basic(freq=random.randint(2, 5), shape=shape, lattice_drift=random.randint(0, 1), hue_range=random.random(), seed=seed, time=time, speed=speed) value_shape = value.value_shape(shape) control = value.convolve(kernel=effects.ValueMask.conv2d_blur, tensor=control, shape=value_shape) with tf.compat.v1.Session().as_default(): tensor = effects.blend_layers(control, shape, random.random() * .5, *collage_images) tensor = value.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, filename) print('mashup')
def vhs(tensor, shape): """ Apply a bad VHS tracking effect. :param Tensor tensor: :param list[int] shape: :return: Tensor """ height, width, channels = shape scan_noise = tf.reshape( basic( [int(height * .5) + 1, int(width * .01) + 1], [height, width, 1]), [height, width]) white_noise = basic( [int(height * .5) + 1, int(width * .1) + 1], [height, width, 1], spline_order=0) # Create horizontal offsets grad = tf.maximum( basic([int(random.random() * 10) + 5, 1], [height, width, 1]) - .5, 0) grad *= grad grad = tf.image.convert_image_dtype(grad, tf.float32, saturate=True) grad = effects.normalize(grad) grad = tf.reshape(grad, [height, width]) tensor = effects.blend_cosine(tensor, white_noise, tf.reshape(grad, [height, width, 1]) * .75) x_index = effects.row_index(shape) - tf.cast( grad * width * .125 + (scan_noise * width * .25 * grad * grad), tf.int32) identity = tf.stack([effects.column_index(shape), x_index], 2) % width tensor = tf.gather_nd(tensor, identity) tensor = tf.image.convert_image_dtype(tensor, tf.float32, saturate=True) 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 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 false_color(tensor, shape, horizontal=False, displacement=.5, **basic_kwargs): """ """ clut = basic(2, shape, **basic_kwargs) return effects.normalize( effects.color_map(tensor, clut, shape, horizontal=horizontal, displacement=displacement))
def frame(ctx, input_dir, frame, seed, filename, width, height): value.set_seed(seed) shape = [height, width, 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, dirname))) if f.endswith('.png')] if not filenames: continue input_filename = os.path.join(input_dir, dirname, 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(), seed=seed, time=frame/30.0, speed=0.125) control = value.value_map(collage_images.pop(), shape, keepdims=True) control = value.convolve(kernel=effects.ValueMask.conv2d_blur, tensor=control, shape=[shape[0], shape[1], 1]) with tf.compat.v1.Session().as_default(): tensor = effects.blend_layers(control, shape, random.random() * .5, *collage_images) tensor = value.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, filename)
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 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 false_color(tensor, shape, horizontal=False, displacement=.5, time=0.0, speed=1.0, **basic_kwargs): """ """ clut = basic(2, shape, time=time, speed=speed, distrib=ValueDistribution.simplex, **basic_kwargs) return effects.normalize( effects.color_map(tensor, clut, shape, horizontal=horizontal, displacement=displacement))
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 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')