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 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 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 basic(freq, shape, ridges=False, sin=0.0, wavelet=False, spline_order=3, distrib=ValueDistribution.normal, corners=False, mask=None, mask_inverse=False, lattice_drift=0.0, rgb=False, hue_range=.125, hue_rotation=None, saturation=1.0, hue_distrib=None, brightness_distrib=None, brightness_freq=None, saturation_distrib=None, speed=1.0, time=0.0, **post_process_args): """ Generate a single layer of scaled noise. .. image:: images/gaussian.jpg :width: 1024 :height: 256 :alt: Noisemaker example output (CC0) :param int|list[int] freq: Base noise frequency. Int, or list of ints for each spatial dimension :param list[int]: Shape of noise. For 2D noise, this is [height, width, channels] :param bool ridges: "Crease" at midpoint values: (1 - abs(n * 2 - 1)) :param float sin: Apply sin function to noise basis :param bool wavelet: Maybe not wavelets this time? :param int spline_order: Spline point count. 0=Constant, 1=Linear, 2=Cosine, 3=Bicubic :param int|str|ValueDistribution distrib: Type of noise distribution. See :class:`ValueDistribution` enum :param bool corners: If True, pin values to corners instead of image center :param None|ValueMask mask: :param bool mask_inverse: :param float lattice_drift: Push away from underlying lattice :param bool rgb: Disable HSV :param float hue_range: HSV hue range :param float|None hue_rotation: HSV hue bias :param float saturation: HSV saturation :param None|int|str|ValueDistribution hue_distrib: Override ValueDistribution for hue :param None|int|str|ValueDistribution saturation_distrib: Override ValueDistribution for saturation :param None|int|str|ValueDistribution brightness_distrib: Override ValueDistribution for brightness :param None|int|list[int] brightness_freq: Override frequency for brightness :param float speed: Displacement range for Z/W axis (simplex only) :param float time: Time argument for Z/W axis (simplex only) :return: Tensor Additional keyword args will be sent to :py:func:`noisemaker.effects.post_process` """ if isinstance(freq, int): freq = effects.freq_for_shape(freq, shape) tensor = values(freq, shape, distrib=distrib, corners=corners, mask=mask, mask_inverse=mask_inverse, spline_order=spline_order, wavelet=wavelet, speed=speed, time=time) if lattice_drift: displacement = lattice_drift / min(freq[0], freq[1]) tensor = effects.refract(tensor, shape, time=time, speed=speed, displacement=displacement, warp_freq=freq, spline_order=spline_order) tensor = effects.post_process(tensor, shape, freq, time=time, speed=speed, spline_order=spline_order, rgb=rgb, **post_process_args) if shape[-1] == 3 and not rgb: if hue_distrib: h = tf.squeeze( values(freq, [shape[0], shape[1], 1], distrib=hue_distrib, corners=corners, mask=mask, mask_inverse=mask_inverse, spline_order=spline_order, wavelet=wavelet, time=time, speed=speed)) else: if hue_rotation is None: hue_rotation = tf.random_normal([]) h = (tensor[:, :, 0] * hue_range + hue_rotation) % 1.0 if saturation_distrib: s = tf.squeeze( values(freq, [shape[0], shape[1], 1], distrib=saturation_distrib, corners=corners, mask=mask, mask_inverse=mask_inverse, spline_order=spline_order, wavelet=wavelet, time=time, speed=speed)) else: s = tensor[:, :, 1] s *= saturation if brightness_distrib or brightness_freq: if isinstance(brightness_freq, int): brightness_freq = effects.freq_for_shape( brightness_freq, shape) v = tf.squeeze( values(brightness_freq or freq, [shape[0], shape[1], 1], distrib=brightness_distrib or ValueDistribution.normal, corners=corners, mask=mask, mask_inverse=mask_inverse, spline_order=spline_order, wavelet=wavelet, time=time, speed=speed)) else: v = tensor[:, :, 2] if ridges and spline_order: # ridges don't work well when not interpolating values v = effects.crease(v) if sin: v = effects.normalize(tf.sin(sin * v)) tensor = tf.image.hsv_to_rgb([tf.stack([h, s, v], 2)])[0] elif ridges and spline_order: tensor = effects.crease(tensor) if sin and rgb: tensor = tf.sin(sin * tensor) return tensor