def generator(ctx, width, height, channels, time, speed, seed, filename, preset_name): if not seed: seed = random.randint(1, MAX_SEED_VALUE) value.set_seed(seed) reload_presets(PRESETS) if preset_name == "random": preset_name = list(GENERATOR_PRESETS)[random.randint( 0, len(GENERATOR_PRESETS) - 1)] print(f"{preset_name} (seed: {seed})") preset = GENERATOR_PRESETS[preset_name] try: preset.render(shape=[height, width, channels], time=time, speed=speed, filename=filename) except Exception as e: util.logger.error( f"preset.render() failed: {e}\nSeed: {seed}\nArgs: {preset.__dict__}" ) raise
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
def animation(ctx, width, height, channels, seed, effect_preset, filename, save_frames, frame_count, watermark, preset_name): if seed is None: seed = random.randint(1, MAX_SEED_VALUE) value.set_seed(seed) reload_presets(PRESETS) if preset_name == 'random': preset_name = list(GENERATOR_PRESETS)[random.randint(0, len(GENERATOR_PRESETS) - 1)] if effect_preset == 'random': effect_preset = list(EFFECT_PRESETS)[random.randint(0, len(EFFECT_PRESETS) - 1)] if effect_preset: print(f"{preset_name} vs. {effect_preset} (seed: {seed})") else: print(f"{preset_name} (seed: {seed})") preset = GENERATOR_PRESETS[preset_name] with tempfile.TemporaryDirectory() as tmp: for i in range(frame_count): frame_filename = f'{tmp}/{i:04d}.png' common_params = ['--seed', str(seed), '--time', f'{i/frame_count:0.4f}', '--filename', frame_filename] util.check_call(['noisemaker', 'generator', preset_name, '--speed', str(_use_reasonable_speed(preset, frame_count)), '--height', str(height), '--width', str(width)] + common_params) if effect_preset: util.check_call(['noisemaker', 'effect', effect_preset, frame_filename, '--no-resize', '--speed', str(_use_reasonable_speed(EFFECT_PRESETS[effect_preset], frame_count))] + common_params) if save_frames: shutil.copy(frame_filename, save_frames) if watermark: util.watermark(watermark, frame_filename) if filename.endswith(".mp4"): # when you want something done right util.check_call(['ffmpeg', '-y', # overwrite existing '-framerate', '30', '-i', f'{tmp}/%04d.png', '-c:v', 'libx264', # because this is what twitter wants '-pix_fmt', 'yuv420p', # because this is what twitter wants '-b:v', '1700000', # maximum allowed bitrate for 720x720 (2048k), minus some encoder overhead '-s', '720x720', # a twitter-recommended size filename]) else: util.magick(f'{tmp}/*png', filename)
import click import tensorflow as tf from noisemaker.composer import EFFECT_PRESETS, GENERATOR_PRESETS, reload_presets from noisemaker.constants import ValueDistribution from noisemaker.presets import PRESETS import noisemaker.cli as cli import noisemaker.generators as generators import noisemaker.effects as effects import noisemaker.util as util import noisemaker.value as value MAX_SEED_VALUE = 2**16 reload_presets(PRESETS) @click.group(help=""" Noisemaker - Let's make generative art with noise https://github.com/aayars/py-noisemaker """, context_settings=cli.CLICK_CONTEXT_SETTINGS) def main(): pass @main.command(help="Generate a .png or .jpg from preset") @cli.width_option(default=2048) @cli.height_option()