From 01e0826bd6f9db54ae31c182aad6e315644c8bbd Mon Sep 17 00:00:00 2001 From: EllangoK Date: Mon, 8 May 2023 20:22:54 -0400 Subject: [PATCH] Adds SineWave Effect --- README.md | 1 + post_processing/kuwahara_blur.py | 1 - post_processing/sine_wave.py | 56 ++++++++++++++++++++++++++++++++ post_processing_nodes.py | 52 ++++++++++++++++++++++++++++- 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 post_processing/sine_wave.py diff --git a/README.md b/README.md index 7922356..9becc40 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Both images have the workflow attached, and are included with the repo. Feel fre - Pixelize: Applies a pixelization effect, simulating the reducing of resolution - $\color{#00A7B5}\textbf{Quantize:}$ Set and dither the amount of colors in an image from 0-256, reducing color information - Sharpen: Enhances the details in an image by applying a sharpening filter + - SineWave: Runs a sine wave through the image, making it appear squiggly - $\color{#00A7B5}\textbf{Solarize:}$ Inverts image colors based on a threshold for a striking, high-contrast effect - Vignette: Applies a vignette effect, putting the corners of the image in shadow diff --git a/post_processing/kuwahara_blur.py b/post_processing/kuwahara_blur.py index aa58794..b06342d 100644 --- a/post_processing/kuwahara_blur.py +++ b/post_processing/kuwahara_blur.py @@ -1,6 +1,5 @@ import cv2 import numpy as np -import multiprocessing as mp import torch class KuwaharaBlur: diff --git a/post_processing/sine_wave.py b/post_processing/sine_wave.py new file mode 100644 index 0000000..fee7d10 --- /dev/null +++ b/post_processing/sine_wave.py @@ -0,0 +1,56 @@ +import torch +import numpy as np + +class SineWave: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "amplitude": ("FLOAT", { + "default": 50, + "min": 0, + "max": 150, + "step": 5 + }), + "frequency": ("FLOAT", { + "default": 5, + "min": 0, + "max": 20, + "step": 1 + }), + }, + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "apply_sine_wave" + + CATEGORY = "postprocessing/Effects" + + def apply_sine_wave(self, image: torch.Tensor, amplitude: float, frequency: float): + batch_size, height, width, channels = image.shape + result = torch.zeros_like(image) + + for b in range(batch_size): + tensor_image = image[b] + result[b] = self.sine_wave_effect(tensor_image, amplitude, frequency) + + return (result,) + + def sine_wave_effect(self, image: torch.Tensor, amplitude: float, frequency: float): + height, width, _ = image.shape + shifted_image = torch.zeros_like(image) + + for channel in range(3): + for i in range(height): + offset = int(amplitude * np.sin(2 * torch.pi * i * frequency / height)) + shifted_image[i, :, channel] = torch.roll(image[i, :, channel], offset) + + return shifted_image + +NODE_CLASS_MAPPINGS = { + "SineWave": SineWave, +} diff --git a/post_processing_nodes.py b/post_processing_nodes.py index e01fed2..c206c57 100644 --- a/post_processing_nodes.py +++ b/post_processing_nodes.py @@ -3,7 +3,6 @@ import torch.nn.functional as F import cv2 import numpy as np from PIL import Image, ImageEnhance -import multiprocessing as mp from PIL import Image @@ -1149,6 +1148,56 @@ class Sharpen: return (result,) +class SineWave: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "amplitude": ("FLOAT", { + "default": 50, + "min": 0, + "max": 150, + "step": 5 + }), + "frequency": ("FLOAT", { + "default": 5, + "min": 0, + "max": 20, + "step": 1 + }), + }, + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "apply_sine_wave" + + CATEGORY = "postprocessing/Effects" + + def apply_sine_wave(self, image: torch.Tensor, amplitude: float, frequency: float): + batch_size, height, width, channels = image.shape + result = torch.zeros_like(image) + + for b in range(batch_size): + tensor_image = image[b] + result[b] = self.sine_wave_effect(tensor_image, amplitude, frequency) + + return (result,) + + def sine_wave_effect(self, image: torch.Tensor, amplitude: float, frequency: float): + height, width, _ = image.shape + shifted_image = torch.zeros_like(image) + + for channel in range(3): + for i in range(height): + offset = int(amplitude * np.sin(2 * torch.pi * i * frequency / height)) + shifted_image[i, :, channel] = torch.roll(image[i, :, channel], offset) + + return shifted_image + class Solarize: def __init__(self): pass @@ -1335,6 +1384,7 @@ NODE_CLASS_MAPPINGS = { "Pixelize": Pixelize, "Quantize": Quantize, "Sharpen": Sharpen, + "SineWave": SineWave, "Solarize": Solarize, "Vignette": Vignette, }