|
import cv2
|
|
import numpy as np
|
|
from PIL import Image
|
|
import torch
|
|
|
|
def pil2tensor(image: Image) -> torch.Tensor:
|
|
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
|
|
|
|
def tensor2pil(t_image: torch.Tensor) -> Image:
|
|
return Image.fromarray(np.clip(255.0 * t_image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
|
|
|
|
def apply_gaussian_blur(image_np, ksize=5, sigmaX=1.0):
|
|
if ksize % 2 == 0:
|
|
ksize += 1
|
|
blurred_image = cv2.GaussianBlur(image_np, (ksize, ksize), sigmaX=sigmaX)
|
|
return blurred_image
|
|
|
|
def apply_guided_filter(image_np, radius, eps):
|
|
|
|
image_np_float = np.float32(image_np) / 255.0
|
|
|
|
filtered_image = cv2.ximgproc.guidedFilter(image_np_float, image_np_float, radius, eps)
|
|
|
|
filtered_image = np.clip(filtered_image * 255, 0, 255).astype(np.uint8)
|
|
return filtered_image
|
|
|
|
class TTPlanet_Tile_Preprocessor_GF:
|
|
def __init__(self, blur_strength=3.0, radius=7, eps=0.01):
|
|
self.blur_strength = blur_strength
|
|
self.radius = radius
|
|
self.eps = eps
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"image": ("IMAGE",),
|
|
"scale_factor": ("FLOAT", {"default": 1.00, "min": 1.00, "max": 8.00, "step": 0.05}),
|
|
"blur_strength": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 10.0, "step": 0.1}),
|
|
"radius": ("INT", {"default": 7, "min": 1, "max": 20, "step": 1}),
|
|
"eps": ("FLOAT", {"default": 0.01, "min": 0.001, "max": 0.1, "step": 0.001}),
|
|
},
|
|
"optional": {}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
RETURN_NAMES = ("image_output",)
|
|
FUNCTION = 'process_image'
|
|
CATEGORY = 'TTP_TILE'
|
|
|
|
def process_image(self, image, scale_factor, blur_strength, radius, eps):
|
|
ret_images = []
|
|
|
|
for i in image:
|
|
|
|
_canvas = tensor2pil(torch.unsqueeze(i, 0)).convert('RGB')
|
|
img_np = np.array(_canvas)[:, :, ::-1]
|
|
|
|
|
|
img_np = apply_gaussian_blur(img_np, ksize=int(blur_strength), sigmaX=blur_strength / 2)
|
|
|
|
|
|
img_np = apply_guided_filter(img_np, radius, eps)
|
|
|
|
|
|
|
|
height, width = img_np.shape[:2]
|
|
new_width = int(width / scale_factor)
|
|
new_height = int(height / scale_factor)
|
|
resized_down = cv2.resize(img_np, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
|
resized_img = cv2.resize(resized_down, (width, height), interpolation=cv2.INTER_CUBIC)
|
|
|
|
|
|
|
|
|
|
pil_img = Image.fromarray(resized_img[:, :, ::-1])
|
|
tensor_img = pil2tensor(pil_img)
|
|
ret_images.append(tensor_img)
|
|
|
|
return (torch.cat(ret_images, dim=0),)
|
|
|
|
class TTPlanet_Tile_Preprocessor_Simple:
|
|
def __init__(self, blur_strength=3.0):
|
|
self.blur_strength = blur_strength
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"image": ("IMAGE",),
|
|
"scale_factor": ("FLOAT", {"default": 2.00, "min": 1.00, "max": 8.00, "step": 0.05}),
|
|
"blur_strength": ("FLOAT", {"default": 1.0, "min": 1.0, "max": 20.0, "step": 0.1}),
|
|
},
|
|
"optional": {}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
RETURN_NAMES = ("image_output",)
|
|
FUNCTION = 'process_image'
|
|
CATEGORY = 'TTP_TILE'
|
|
|
|
def process_image(self, image, scale_factor, blur_strength):
|
|
ret_images = []
|
|
|
|
for i in image:
|
|
|
|
_canvas = tensor2pil(torch.unsqueeze(i, 0)).convert('RGB')
|
|
|
|
|
|
img_np = np.array(_canvas)[:, :, ::-1]
|
|
|
|
|
|
height, width = img_np.shape[:2]
|
|
new_width = int(width / scale_factor)
|
|
new_height = int(height / scale_factor)
|
|
resized_down = cv2.resize(img_np, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
|
resized_img = cv2.resize(resized_down, (width, height), interpolation=cv2.INTER_LANCZOS4)
|
|
|
|
|
|
img_np = apply_gaussian_blur(resized_img, ksize=int(blur_strength), sigmaX=blur_strength / 2)
|
|
|
|
|
|
_canvas = Image.fromarray(img_np[:, :, ::-1])
|
|
tensor_img = pil2tensor(_canvas)
|
|
ret_images.append(tensor_img)
|
|
|
|
return (torch.cat(ret_images, dim=0),)
|
|
|
|
class TTPlanet_Tile_Preprocessor_cufoff:
|
|
def __init__(self, blur_strength=3.0, cutoff_frequency=30, filter_strength=1.0):
|
|
self.blur_strength = blur_strength
|
|
self.cutoff_frequency = cutoff_frequency
|
|
self.filter_strength = filter_strength
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"image": ("IMAGE",),
|
|
"scale_factor": ("FLOAT", {"default": 1.00, "min": 1.00, "max": 8.00, "step": 0.05}),
|
|
"blur_strength": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 10.0, "step": 0.1}),
|
|
"cutoff_frequency": ("INT", {"default": 100, "min": 0, "max": 256, "step": 1}),
|
|
"filter_strength": ("FLOAT", {"default": 1.0, "min": 0.1, "max": 10.0, "step": 0.1}),
|
|
},
|
|
"optional": {}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
RETURN_NAMES = ("image_output",)
|
|
FUNCTION = 'process_image'
|
|
CATEGORY = 'TTP_TILE'
|
|
|
|
def process_image(self, image, scale_factor, blur_strength, cutoff_frequency, filter_strength):
|
|
ret_images = []
|
|
|
|
for i in image:
|
|
|
|
_canvas = tensor2pil(torch.unsqueeze(i, 0)).convert('RGB')
|
|
img_np = np.array(_canvas)[:, :, ::-1]
|
|
|
|
|
|
img_np = apply_low_pass_filter(img_np, cutoff_frequency, filter_strength)
|
|
|
|
|
|
height, width = img_np.shape[:2]
|
|
new_width = int(width / scale_factor)
|
|
new_height = int(height / scale_factor)
|
|
resized_down = cv2.resize(img_np, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
|
resized_img = cv2.resize(resized_down, (width, height), interpolation=cv2.INTER_LANCZOS4)
|
|
|
|
|
|
img_np = apply_gaussian_blur(img_np, ksize=int(blur_strength), sigmaX=blur_strength / 2)
|
|
|
|
|
|
pil_img = Image.fromarray(resized_img[:, :, ::-1])
|
|
tensor_img = pil2tensor(pil_img)
|
|
ret_images.append(tensor_img)
|
|
|
|
return (torch.cat(ret_images, dim=0),)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"TTPlanet_Tile_Preprocessor_GF": TTPlanet_Tile_Preprocessor_GF,
|
|
"TTPlanet_Tile_Preprocessor_Simple": TTPlanet_Tile_Preprocessor_Simple,
|
|
"TTPlanet_Tile_Preprocessor_cufoff": TTPlanet_Tile_Preprocessor_cufoff
|
|
}
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
"TTPlanet_Tile_Preprocessor_GF": "🪐TTPlanet Tile Preprocessor GF",
|
|
"TTPlanet_Tile_Preprocessor_Simple": "🪐TTPlanet Tile Preprocessor Simple",
|
|
"TTPlanet_Tile_Preprocessor_cufoff": "🪐TTPlanet Tile Preprocessor cufoff"
|
|
} |