Files
John Richard Chipps-Harding 10e5d4062d Change Vignette behaviour on Film Grain node
Simplified the vignette functionality on the Film Grain node to support floats (step: 0.01).

This allows very subtle vignette.
2024-02-07 01:57:51 +00:00

152 lines
4.6 KiB
Python

import numpy as np
import torch
class FilmGrain:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"intensity": ("FLOAT", {
"default": 0.2,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"scale": ("FLOAT", {
"default": 10,
"min": 1,
"max": 100,
"step": 1
}),
"temperature": ("FLOAT", {
"default": 0.0,
"min": -100,
"max": 100,
"step": 1
}),
"vignette": ("FLOAT", {
"default": 0.0,
"min": 0.0,
"max": 10.0,
"step": 0.01
}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "film_grain"
CATEGORY = "postprocessing/Effects"
def film_grain(self, image: torch.Tensor, intensity: float, scale: float, temperature: float, vignette: float):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
for b in range(batch_size):
tensor_image = image[b].numpy()
# Generate Perlin noise with shape (height, width) and scale
noise = self.generate_perlin_noise((height, width), scale)
noise = (noise - np.min(noise)) / (np.max(noise) - np.min(noise))
# Apply grain intensity
noise = (noise * 2 - 1) * intensity
# Blend the noise with the image
grain_image = np.clip(tensor_image + noise[:, :, np.newaxis], 0, 1)
# Apply temperature
grain_image = self.apply_temperature(grain_image, temperature)
# Apply vignette
grain_image = self.apply_vignette(grain_image, vignette)
tensor = torch.from_numpy(grain_image).unsqueeze(0)
result[b] = tensor
return (result,)
def generate_perlin_noise(self, shape, scale, octaves=4, persistence=0.5, lacunarity=2):
def smoothstep(t):
return t * t * (3.0 - 2.0 * t)
def lerp(t, a, b):
return a + t * (b - a)
def gradient(h, x, y):
vectors = np.array([[1, 1], [-1, 1], [1, -1], [-1, -1]])
g = vectors[h % 4]
return g[:, :, 0] * x + g[:, :, 1] * y
height, width = shape
noise = np.zeros(shape)
for octave in range(octaves):
octave_scale = scale * lacunarity ** octave
x = np.linspace(0, 1, width, endpoint=False)
y = np.linspace(0, 1, height, endpoint=False)
X, Y = np.meshgrid(x, y)
X, Y = X * octave_scale, Y * octave_scale
xi = X.astype(int)
yi = Y.astype(int)
xf = X - xi
yf = Y - yi
u = smoothstep(xf)
v = smoothstep(yf)
n00 = gradient(np.random.randint(0, 4, (height, width)), xf, yf)
n01 = gradient(np.random.randint(0, 4, (height, width)), xf, yf - 1)
n10 = gradient(np.random.randint(0, 4, (height, width)), xf - 1, yf)
n11 = gradient(np.random.randint(0, 4, (height, width)), xf - 1, yf - 1)
x1 = lerp(u, n00, n10)
x2 = lerp(u, n01, n11)
y1 = lerp(v, x1, x2)
noise += y1 * persistence ** octave
return noise / (1 - persistence ** octaves)
def apply_temperature(self, image, temperature):
if temperature == 0:
return image
temperature /= 100
new_image = image.copy()
if temperature > 0:
new_image[:, :, 0] *= 1 + temperature
new_image[:, :, 1] *= 1 + temperature * 0.4
else:
new_image[:, :, 2] *= 1 - temperature
return np.clip(new_image, 0, 1)
def apply_vignette(self, image, vignette_strength):
if vignette_strength == 0:
return image
height, width, _ = image.shape
x = np.linspace(-1, 1, width)
y = np.linspace(-1, 1, height)
X, Y = np.meshgrid(x, y)
radius = np.sqrt(X ** 2 + Y ** 2)
radius = radius / np.max(radius)
opacity = np.clip(vignette_strength, 0, 1)
vignette = 1 - radius * opacity
return np.clip(image * vignette[..., np.newaxis], 0, 1)
NODE_CLASS_MAPPINGS = {
"FilmGrain": FilmGrain,
}