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 _control(): shape = [SMALL_Y, SMALL_X, 1] control = generators.multires(shape=shape, freq=FREQ, octaves=OCTAVES, refract_range=.5) erode_kwargs = { "alpha": .025, "density": 40, "iterations": 20, "inverse": True, } iterations = 5 for i in range(iterations): control = effects.erode(control, shape, **erode_kwargs) control = effects.convolve(constants.ValueMask.conv2d_blur, control, shape) post_shape = [LARGE_Y, LARGE_X, 1] control = effects.resample(control, post_shape) iterations = 2 for i in range(iterations): control = effects.erode(control, post_shape, **erode_kwargs) control = effects.convolve(constants.ValueMask.conv2d_blur, control, post_shape) control = effects.convolve(constants.ValueMask.conv2d_sharpen, control, post_shape) control = effects.normalize(control) with tf.Session().as_default(): save(control, CONTROL_FILENAME)
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 main(ctx, name, input_filename): tensor = tf.image.convert_image_dtype(load(input_filename), tf.float32) max_height = 1024 max_width = 1024 with tf.Session().as_default(): height, width, channels = tf.shape(tensor).eval() need_resample = False if height != width: length = min(height, width) height = length width = length tensor = tf.image.resize_image_with_crop_or_pad(tensor, length, length) if height > max_height: need_resample = True width = int(width * (max_height / height)) height = max_height if width > max_width: need_resample = True height = int(height * (max_width / width)) width = max_width shape = [height, width, channels] if need_resample: tensor = effects.resample(tensor, shape) save(tensor, name)
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 main(ctx, seed, name, preset_name, input_filename): generators.set_seed(seed) tensor = tf.image.convert_image_dtype(load(input_filename), tf.float32) max_height = 1024 max_width = 1024 with tf.Session().as_default(): height, width, channels = tf.shape(tensor).eval() need_resample = False # Some presets only like square images. Work around for now by cropping. if height != width: length = min(height, width) height = length width = length tensor = tf.image.resize_image_with_crop_or_pad(tensor, length, length) if height > max_height: need_resample = True width = int(width * (max_height / height)) height = max_height if width > max_width: need_resample = True height = int(height * (max_width / width)) width = max_width shape = [height, width, channels] if need_resample: tensor = effects.resample(tensor, shape) kwargs, preset_name = presets.load(preset_name, presets.EFFECTS_PRESETS()) kwargs["shape"] = [height, width, channels] 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 tensor = effects.post_process(tensor, **kwargs) tensor = recipes.post_process(tensor, **kwargs) print(preset_name) save(tensor, name)
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 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 main(ctx, name, retro_upscale, input_filename): shape = effects.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 = effects.resample(tensor, shape, spline_order=0) tensor = effects.square_crop_and_resize(tensor, shape, 1024) with tf.Session().as_default(): save(tensor, name)
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 render(ctx, glitch, vhs, crt, scan_error, snow, dither, aberration, bloom, name, input_filename): tensor = tf.image.convert_image_dtype(load(input_filename), tf.float32) freq = [3, 3] max_height = 1024 max_width = 1024 with tf.Session().as_default(): height, width, channels = tf.shape(tensor).eval() need_resample = False if height > max_height: need_resample = True width = int(width * (max_height / height)) height = max_height if width > max_width: need_resample = True height = int(height * (max_width / width)) width = max_width shape = [height, width, channels] if need_resample: tensor = effects.resample(tensor, shape) tensor = effects.post_process(tensor, shape=shape, freq=freq, with_bloom=bloom, with_aberration=aberration) tensor = recipes.post_process(tensor, shape=shape, freq=freq, with_glitch=glitch, with_vhs=vhs, with_crt=crt, with_scan_error=scan_error, with_snow=snow, with_dither=dither) save(tensor, name) print(name)
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')
def values(freq, shape, distrib=ValueDistribution.normal, corners=False, mask=None, mask_inverse=False, spline_order=3, wavelet=False, time=0.0, speed=1.0): """ """ initial_shape = freq + [shape[-1]] if isinstance(distrib, int): distrib = ValueDistribution(distrib) elif isinstance(distrib, str): distrib = ValueDistribution[distrib] if isinstance(mask, int): mask = ValueMask(mask) elif isinstance(mask, str): mask = ValueMask[mask] if distrib == ValueDistribution.ones: tensor = tf.ones(initial_shape) elif distrib == ValueDistribution.mids: tensor = tf.ones(initial_shape) * .5 elif distrib == ValueDistribution.normal: tensor = tf.random_normal(initial_shape) elif distrib == ValueDistribution.uniform: tensor = tf.random_uniform(initial_shape) elif distrib == ValueDistribution.exp: tensor = tf.cast(tf.stack(np.random.exponential(size=initial_shape)), tf.float32) elif distrib == ValueDistribution.laplace: tensor = tf.cast(tf.stack(np.random.laplace(size=initial_shape)), tf.float32) elif distrib == ValueDistribution.lognormal: tensor = tf.cast(tf.stack(np.random.lognormal(size=initial_shape)), tf.float32) elif distrib == ValueDistribution.column_index: tensor = tf.expand_dims( tf.cast(effects.normalize(effects.column_index(initial_shape)), tf.float32), -1) * tf.ones(initial_shape, tf.float32) elif distrib == ValueDistribution.row_index: tensor = tf.expand_dims( tf.cast(effects.normalize(effects.row_index(initial_shape)), tf.float32), -1) * tf.ones(initial_shape, tf.float32) elif distrib == ValueDistribution.simplex: tensor = simplex.simplex(initial_shape, time=time, speed=speed) elif distrib == ValueDistribution.simplex_exp: tensor = tf.pow(simplex.simplex(initial_shape, time=time, speed=speed), 4) else: raise ValueError("%s (%s) is not a ValueDistribution" % (distrib, type(distrib))) if mask: atlas = None if mask == ValueMask.truetype: from noisemaker.glyphs import load_glyphs atlas = load_glyphs([15, 15, 1]) if not atlas: mask = ValueMask.numeric # Fall back to canned values channel_shape = freq + [1] mask_values, _ = masks.mask_values(mask, channel_shape, atlas=atlas, inverse=mask_inverse, time=time, speed=speed) tensor *= mask_values if wavelet: tensor = effects.wavelet(tensor, initial_shape) tensor = effects.resample(tensor, shape, spline_order=spline_order) if (not corners and (freq[0] % 2) == 0) or (corners and (freq[0] % 2) == 1): tensor = effects.offset(tensor, shape, x=int((shape[1] / freq[1]) * .5), y=int((shape[0] / freq[0]) * .5)) return tensor