From 8e5f00fdd80ce19b3d2a829120ee2f79f17d198e Mon Sep 17 00:00:00 2001 From: EllangoK Date: Fri, 31 Mar 2023 17:15:15 -0400 Subject: [PATCH] adds glow effect --- README.md | 1 + post_processing/glow.py | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 post_processing/glow.py diff --git a/README.md b/README.md index 6c2ac87..440c04b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ A collection of post processing nodes for [ComfyUI](https://github.com/comfyanon - ColorCorrect: Adjusts the color balance, temperature, hue, brightness, contrast, saturation, and gamma of an image - Dither: Reduces the color information in an image by dithering, resulting in a patterned, pixelated appearance - FilmGrain: Adds a film grain effect to the image, along with options to control the temperature, and vignetting + - Glow: Applies a blur with a specified radius and then blends it with the original image. Creates a nice glowing effect. - GaussianBlur: Applies a Gaussian blur to the input image, softening the details - KMeansQuantize: Reduce the amount of colors in an image from 0-256 - PixelSort: Rearranges the pixels in the input image based on their values, and input mask. Creates a cool glitch like effect. diff --git a/post_processing/glow.py b/post_processing/glow.py new file mode 100644 index 0000000..40fb7eb --- /dev/null +++ b/post_processing/glow.py @@ -0,0 +1,62 @@ +import torch +import torch.nn.functional as F + +class Glow: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "image": ("IMAGE",), + "intensity": ("FLOAT", { + "default": 1.0, + "min": 0.0, + "max": 5.0, + "step": 0.01 + }), + "blur_radius": ("INT", { + "default": 5, + "min": 1, + "max": 50, + "step": 1 + }), + }, + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "apply_glow" + + CATEGORY = "postprocessing" + + def apply_glow(self, image: torch.Tensor, intensity: float, blur_radius: int): + blurred_image = self.gaussian_blur(image, 2 * blur_radius + 1) + glowing_image = self.add_glow(image, blurred_image, intensity) + glowing_image = torch.clamp(glowing_image, 0, 1) + return (glowing_image,) + + def gaussian_kernel(self, kernel_size: int): + sigma = (kernel_size - 1) / 6 + x, y = torch.meshgrid(torch.linspace(-1, 1, kernel_size), torch.linspace(-1, 1, kernel_size)) + d = torch.sqrt(x * x + y * y) + g = torch.exp(-(d * d) / (2.0 * sigma * sigma)) + return g / g.sum() + + def gaussian_blur(self, image: torch.Tensor, kernel_size: int): + batch_size, height, width, channels = image.shape + + kernel = self.gaussian_kernel(kernel_size).repeat(channels, 1, 1).unsqueeze(1) + + image = image.permute(0, 3, 1, 2) # Torch wants (B, C, H, W) we use (B, H, W, C) + blurred = F.conv2d(image, kernel, padding=kernel_size // 2, groups=channels) + blurred = blurred.permute(0, 2, 3, 1) + + return blurred + + def add_glow(self, img, blurred_img, intensity): + return img + blurred_img * intensity + +NODE_CLASS_MAPPINGS = { + "Glow": Glow, +}