better pre-processor for comfyui
Browse filesupdate the pre-processor to better version!
- TTP_tile_preprocessor_v5.py +191 -0
TTP_tile_preprocessor_v5.py
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
def pil2tensor(image: Image) -> torch.Tensor:
|
7 |
+
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
|
8 |
+
|
9 |
+
def tensor2pil(t_image: torch.Tensor) -> Image:
|
10 |
+
return Image.fromarray(np.clip(255.0 * t_image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
|
11 |
+
|
12 |
+
def apply_gaussian_blur(image_np, ksize=5, sigmaX=1.0):
|
13 |
+
if ksize % 2 == 0:
|
14 |
+
ksize += 1 # ksize must be odd
|
15 |
+
blurred_image = cv2.GaussianBlur(image_np, (ksize, ksize), sigmaX=sigmaX)
|
16 |
+
return blurred_image
|
17 |
+
|
18 |
+
def apply_guided_filter(image_np, radius, eps):
|
19 |
+
# Convert image to float32 for the guided filter
|
20 |
+
image_np_float = np.float32(image_np) / 255.0
|
21 |
+
# Apply the guided filter
|
22 |
+
filtered_image = cv2.ximgproc.guidedFilter(image_np_float, image_np_float, radius, eps)
|
23 |
+
# Scale back to uint8
|
24 |
+
filtered_image = np.clip(filtered_image * 255, 0, 255).astype(np.uint8)
|
25 |
+
return filtered_image
|
26 |
+
|
27 |
+
class TTPlanet_Tile_Preprocessor_GF:
|
28 |
+
def __init__(self, blur_strength=3.0, radius=7, eps=0.01):
|
29 |
+
self.blur_strength = blur_strength
|
30 |
+
self.radius = radius
|
31 |
+
self.eps = eps
|
32 |
+
|
33 |
+
@classmethod
|
34 |
+
def INPUT_TYPES(cls):
|
35 |
+
return {
|
36 |
+
"required": {
|
37 |
+
"image": ("IMAGE",),
|
38 |
+
"scale_factor": ("FLOAT", {"default": 1.00, "min": 1.00, "max": 8.00, "step": 0.05}),
|
39 |
+
"blur_strength": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 10.0, "step": 0.1}),
|
40 |
+
"radius": ("INT", {"default": 7, "min": 1, "max": 20, "step": 1}),
|
41 |
+
"eps": ("FLOAT", {"default": 0.01, "min": 0.001, "max": 0.1, "step": 0.001}),
|
42 |
+
},
|
43 |
+
"optional": {}
|
44 |
+
}
|
45 |
+
|
46 |
+
RETURN_TYPES = ("IMAGE",)
|
47 |
+
RETURN_NAMES = ("image_output",)
|
48 |
+
FUNCTION = 'process_image'
|
49 |
+
CATEGORY = 'TTP_TILE'
|
50 |
+
|
51 |
+
def process_image(self, image, scale_factor, blur_strength, radius, eps):
|
52 |
+
ret_images = []
|
53 |
+
|
54 |
+
for i in image:
|
55 |
+
# Convert tensor to PIL for processing
|
56 |
+
_canvas = tensor2pil(torch.unsqueeze(i, 0)).convert('RGB')
|
57 |
+
img_np = np.array(_canvas)[:, :, ::-1] # RGB to BGR
|
58 |
+
|
59 |
+
# Apply Gaussian blur
|
60 |
+
img_np = apply_gaussian_blur(img_np, ksize=int(blur_strength), sigmaX=blur_strength / 2)
|
61 |
+
|
62 |
+
# Apply Guided Filter
|
63 |
+
img_np = apply_guided_filter(img_np, radius, eps)
|
64 |
+
|
65 |
+
|
66 |
+
# Resize image
|
67 |
+
height, width = img_np.shape[:2]
|
68 |
+
new_width = int(width / scale_factor)
|
69 |
+
new_height = int(height / scale_factor)
|
70 |
+
resized_down = cv2.resize(img_np, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
71 |
+
resized_img = cv2.resize(resized_down, (width, height), interpolation=cv2.INTER_CUBIC)
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
# Convert OpenCV back to PIL and then to tensor
|
76 |
+
pil_img = Image.fromarray(resized_img[:, :, ::-1]) # BGR to RGB
|
77 |
+
tensor_img = pil2tensor(pil_img)
|
78 |
+
ret_images.append(tensor_img)
|
79 |
+
|
80 |
+
return (torch.cat(ret_images, dim=0),)
|
81 |
+
|
82 |
+
class TTPlanet_Tile_Preprocessor_Simple:
|
83 |
+
def __init__(self, blur_strength=3.0):
|
84 |
+
self.blur_strength = blur_strength
|
85 |
+
|
86 |
+
@classmethod
|
87 |
+
def INPUT_TYPES(cls):
|
88 |
+
return {
|
89 |
+
"required": {
|
90 |
+
"image": ("IMAGE",),
|
91 |
+
"scale_factor": ("FLOAT", {"default": 2.00, "min": 1.00, "max": 8.00, "step": 0.05}),
|
92 |
+
"blur_strength": ("FLOAT", {"default": 1.0, "min": 1.0, "max": 20.0, "step": 0.1}),
|
93 |
+
},
|
94 |
+
"optional": {}
|
95 |
+
}
|
96 |
+
|
97 |
+
RETURN_TYPES = ("IMAGE",)
|
98 |
+
RETURN_NAMES = ("image_output",)
|
99 |
+
FUNCTION = 'process_image'
|
100 |
+
CATEGORY = 'TTP_TILE'
|
101 |
+
|
102 |
+
def process_image(self, image, scale_factor, blur_strength):
|
103 |
+
ret_images = []
|
104 |
+
|
105 |
+
for i in image:
|
106 |
+
# Convert tensor to PIL for processing
|
107 |
+
_canvas = tensor2pil(torch.unsqueeze(i, 0)).convert('RGB')
|
108 |
+
|
109 |
+
# Convert PIL image to OpenCV format
|
110 |
+
img_np = np.array(_canvas)[:, :, ::-1] # RGB to BGR
|
111 |
+
|
112 |
+
# Resize image first if you want blur to apply after resizing
|
113 |
+
height, width = img_np.shape[:2]
|
114 |
+
new_width = int(width / scale_factor)
|
115 |
+
new_height = int(height / scale_factor)
|
116 |
+
resized_down = cv2.resize(img_np, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
117 |
+
resized_img = cv2.resize(resized_down, (width, height), interpolation=cv2.INTER_LANCZOS4)
|
118 |
+
|
119 |
+
# Apply Gaussian blur after resizing
|
120 |
+
img_np = apply_gaussian_blur(resized_img, ksize=int(blur_strength), sigmaX=blur_strength / 2)
|
121 |
+
|
122 |
+
# Convert OpenCV back to PIL and then to tensor
|
123 |
+
_canvas = Image.fromarray(img_np[:, :, ::-1]) # BGR to RGB
|
124 |
+
tensor_img = pil2tensor(_canvas)
|
125 |
+
ret_images.append(tensor_img)
|
126 |
+
|
127 |
+
return (torch.cat(ret_images, dim=0),)
|
128 |
+
|
129 |
+
class TTPlanet_Tile_Preprocessor_cufoff:
|
130 |
+
def __init__(self, blur_strength=3.0, cutoff_frequency=30, filter_strength=1.0):
|
131 |
+
self.blur_strength = blur_strength
|
132 |
+
self.cutoff_frequency = cutoff_frequency
|
133 |
+
self.filter_strength = filter_strength
|
134 |
+
|
135 |
+
@classmethod
|
136 |
+
def INPUT_TYPES(cls):
|
137 |
+
return {
|
138 |
+
"required": {
|
139 |
+
"image": ("IMAGE",),
|
140 |
+
"scale_factor": ("FLOAT", {"default": 1.00, "min": 1.00, "max": 8.00, "step": 0.05}),
|
141 |
+
"blur_strength": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 10.0, "step": 0.1}),
|
142 |
+
"cutoff_frequency": ("INT", {"default": 100, "min": 0, "max": 256, "step": 1}),
|
143 |
+
"filter_strength": ("FLOAT", {"default": 1.0, "min": 0.1, "max": 10.0, "step": 0.1}),
|
144 |
+
},
|
145 |
+
"optional": {}
|
146 |
+
}
|
147 |
+
|
148 |
+
RETURN_TYPES = ("IMAGE",)
|
149 |
+
RETURN_NAMES = ("image_output",)
|
150 |
+
FUNCTION = 'process_image'
|
151 |
+
CATEGORY = 'TTP_TILE'
|
152 |
+
|
153 |
+
def process_image(self, image, scale_factor, blur_strength, cutoff_frequency, filter_strength):
|
154 |
+
ret_images = []
|
155 |
+
|
156 |
+
for i in image:
|
157 |
+
# Convert tensor to PIL for processing
|
158 |
+
_canvas = tensor2pil(torch.unsqueeze(i, 0)).convert('RGB')
|
159 |
+
img_np = np.array(_canvas)[:, :, ::-1] # RGB to BGR
|
160 |
+
|
161 |
+
# Apply low pass filter with new strength parameter
|
162 |
+
img_np = apply_low_pass_filter(img_np, cutoff_frequency, filter_strength)
|
163 |
+
|
164 |
+
# Resize image
|
165 |
+
height, width = img_np.shape[:2]
|
166 |
+
new_width = int(width / scale_factor)
|
167 |
+
new_height = int(height / scale_factor)
|
168 |
+
resized_down = cv2.resize(img_np, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
169 |
+
resized_img = cv2.resize(resized_down, (width, height), interpolation=cv2.INTER_LANCZOS4)
|
170 |
+
|
171 |
+
# Apply Gaussian blur
|
172 |
+
img_np = apply_gaussian_blur(img_np, ksize=int(blur_strength), sigmaX=blur_strength / 2)
|
173 |
+
|
174 |
+
# Convert OpenCV back to PIL and then to tensor
|
175 |
+
pil_img = Image.fromarray(resized_img[:, :, ::-1]) # BGR to RGB
|
176 |
+
tensor_img = pil2tensor(pil_img)
|
177 |
+
ret_images.append(tensor_img)
|
178 |
+
|
179 |
+
return (torch.cat(ret_images, dim=0),)
|
180 |
+
|
181 |
+
NODE_CLASS_MAPPINGS = {
|
182 |
+
"TTPlanet_Tile_Preprocessor_GF": TTPlanet_Tile_Preprocessor_GF,
|
183 |
+
"TTPlanet_Tile_Preprocessor_Simple": TTPlanet_Tile_Preprocessor_Simple,
|
184 |
+
"TTPlanet_Tile_Preprocessor_cufoff": TTPlanet_Tile_Preprocessor_cufoff
|
185 |
+
}
|
186 |
+
|
187 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
188 |
+
"TTPlanet_Tile_Preprocessor_GF": "🪐TTPlanet Tile Preprocessor GF",
|
189 |
+
"TTPlanet_Tile_Preprocessor_Simple": "🪐TTPlanet Tile Preprocessor Simple",
|
190 |
+
"TTPlanet_Tile_Preprocessor_cufoff": "🪐TTPlanet Tile Preprocessor cufoff"
|
191 |
+
}
|