-
Notifications
You must be signed in to change notification settings - Fork 10
/
nodes.py
530 lines (421 loc) · 18 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import sys
import os
import cv2
import torch
import numpy as np
import folder_paths
from .utils import processing as processing_utils
from .utils import loading as loading_utils
# Get the directory of the current file and add it to the system path
current_file_directory = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_file_directory)
import filmgrainer.filmgrainer as filmgrainer
# Create the directory for the LUTs
dir_luts = os.path.join(folder_paths.models_dir, "luts")
os.makedirs(dir_luts, exist_ok=True)
folder_paths.folder_names_and_paths["luts"] = ([dir_luts], set(['.cube']))
class ProPostVignette:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"intensity": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 10.0,
"step": 0.01
}),
"center_x": ("FLOAT", {
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"center_y": ("FLOAT", {
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ()
FUNCTION = "vignette_image"
#OUTPUT_NODE = False
CATEGORY = "Pro Post/Camera Effects"
def vignette_image(self, image: torch.Tensor, intensity: float, center_x: float, center_y: float):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
if intensity == 0:
return image
# Generate the vignette for each image in the batch
# Create linear space but centered around the provided center point ratios
x = np.linspace(-1, 1, width)
y = np.linspace(-1, 1, height)
X, Y = np.meshgrid(x - (2 * center_x - 1), y - (2 * center_y - 1))
# Calculate distances to the furthest corner
distances_to_corners = [
np.sqrt((0 - center_x) ** 2 + (0 - center_y) ** 2),
np.sqrt((1 - center_x) ** 2 + (0 - center_y) ** 2),
np.sqrt((0 - center_x) ** 2 + (1 - center_y) ** 2),
np.sqrt((1 - center_x) ** 2 + (1 - center_y) ** 2)
]
max_distance_to_corner = np.max(distances_to_corners)
radius = np.sqrt(X ** 2 + Y ** 2)
radius = radius / (max_distance_to_corner * np.sqrt(2)) # Normalize radius
opacity = np.clip(intensity, 0, 1)
vignette = 1 - radius * opacity
for b in range(batch_size):
tensor_image = image[b].numpy()
# Apply vignette
vignette_image = self.apply_vignette(tensor_image, vignette)
tensor = torch.from_numpy(vignette_image).unsqueeze(0)
result[b] = tensor
return (result,)
def apply_vignette(self, image, vignette):
# If image needs to be normalized (0-1 range)
needs_normalization = image.max() > 1
if needs_normalization:
image = image.astype(np.float32) / 255
final_image = np.clip(image * vignette[..., np.newaxis], 0, 1)
if needs_normalization:
final_image = (final_image * 255).astype(np.uint8)
return final_image
class ProPostFilmGrain:
grain_types = ["Fine", "Fine Simple", "Coarse", "Coarser"]
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"gray_scale": ("BOOLEAN", {
"default": False
}),
"grain_type": (s.grain_types,),
"grain_sat": ("FLOAT", {
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"grain_power": ("FLOAT", {
"default": 0.7,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"shadows": ("FLOAT", {
"default": 0.2,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"highs": ("FLOAT", {
"default": 0.2,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"scale": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 10.0,
"step": 0.01
}),
"sharpen": ("INT", {
"default": 0,
"min": 0,
"max": 10
}),
"src_gamma": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 10.0,
"step": 0.01
}),
"seed": ("INT", {
"default": 1,
"min": 1,
"max": 1000
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ()
FUNCTION = "filmgrain_image"
#OUTPUT_NODE = False
CATEGORY = "Pro Post/Camera Effects"
def filmgrain_image(self, image: torch.Tensor, gray_scale: bool, grain_type: str, grain_sat: float, grain_power: float, shadows: float, highs: float, scale: float, sharpen: int, src_gamma: float, seed: int):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
# find index of grain_type
grain_type_index = self.grain_types.index(grain_type) + 1;
for b in range(batch_size):
tensor_image = image[b].numpy()
# Apply vignette
vignette_image = self.apply_filmgrain(tensor_image, gray_scale, grain_type_index, grain_sat, grain_power, shadows, highs, scale, sharpen, src_gamma, seed+b)
tensor = torch.from_numpy(vignette_image).unsqueeze(0)
result[b] = tensor
return (result,)
def apply_filmgrain(self, image, gray_scale, grain_type, grain_sat, grain_power, shadows, highs, scale, sharpen, src_gamma, seed):
out_image = filmgrainer.process(image, scale, src_gamma,
grain_power, shadows, highs, grain_type,
grain_sat, gray_scale, sharpen, seed)
return out_image
class ProPostRadialBlur:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"blur_strength": ("FLOAT", {
"default": 64.0,
"min": 0.0,
"max": 256.0,
"step": 1.0
}),
"center_x": ("FLOAT", {
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"center_y": ("FLOAT", {
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"focus_spread": ("FLOAT", {
"default": 1,
"min": 0.1,
"max": 8.0,
"step": 0.1
}),
"steps": ("INT", {
"default": 5,
"min": 1,
"max": 32,
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ()
FUNCTION = "radialblur_image"
#OUTPUT_NODE = False
CATEGORY = "Pro Post/Blur Effects"
def radialblur_image(self, image: torch.Tensor, blur_strength: float, center_x: float, center_y:float, focus_spread:float, steps: int):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
# Generate the vignette for each image in the batch
c_x, c_y = int(width * center_x), int(height * center_y)
# Calculate distances to all corners from the center
distances_to_corners = [
np.sqrt((c_x - 0)**2 + (c_y - 0)**2),
np.sqrt((c_x - width)**2 + (c_y - 0)**2),
np.sqrt((c_x - 0)**2 + (c_y - height)**2),
np.sqrt((c_x - width)**2 + (c_y - height)**2)
]
max_distance_to_corner = max(distances_to_corners)
# Create and adjust radial mask
X, Y = np.meshgrid(np.arange(width) - c_x, np.arange(height) - c_y)
radial_mask = np.sqrt(X**2 + Y**2) / max_distance_to_corner
for b in range(batch_size):
tensor_image = image[b].numpy()
# Apply blur
blur_image = self.apply_radialblur(tensor_image, blur_strength, radial_mask, focus_spread, steps)
tensor = torch.from_numpy(blur_image).unsqueeze(0)
result[b] = tensor
return (result,)
def apply_radialblur(self, image, blur_strength, radial_mask, focus_spread, steps):
needs_normalization = image.max() > 1
if needs_normalization:
image = image.astype(np.float32) / 255
blurred_images = processing_utils.generate_blurred_images(image, blur_strength, steps, focus_spread)
final_image = processing_utils.apply_blurred_images(image, blurred_images, radial_mask)
if needs_normalization:
final_image = np.clip(final_image * 255, 0, 255).astype(np.uint8)
return final_image
class ProPostDepthMapBlur:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"depth_map": ("IMAGE",),
"blur_strength": ("FLOAT", {
"default": 64.0,
"min": 0.0,
"max": 256.0,
"step": 1.0
}),
"focal_depth": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"focus_spread": ("FLOAT", {
"default": 1,
"min": 1.0,
"max": 8.0,
"step": 0.1
}),
"steps": ("INT", {
"default": 5,
"min": 1,
"max": 32,
}),
"focal_range": ("FLOAT", {
"default": 0.0,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"mask_blur": ("INT", {
"default": 1,
"min": 1,
"max": 127,
"step": 2
}),
},
}
RETURN_TYPES = ("IMAGE","MASK")
RETURN_NAMES = ()
FUNCTION = "depthblur_image"
DESCRIPTION = """
blur_strength: Represents the blur strength. This parameter controls the overall intensity of the blur effect; the higher the value, the more blurred the image becomes.
focal_depth: Represents the focal depth. This parameter is used to determine which depth level in the image should remain sharp, while other levels are blurred based on depth differences.
focus_spread: Represents the focus spread range. This parameter controls the size of the blur transition area near the focal depth; the larger the value, the wider the transition area, and the smoother the blur effect spreads around the focus.
steps: Represents the number of steps in the blur process. This parameter determines the calculation precision of the blur effect; the more steps, the finer the blur effect, but this also increases the computational load.
focal_range: Represents the focal range. This parameter is used to adjust the depth range within the focal depth that remains sharp; the larger the value, the wider the area around the focal depth that remains sharp.
mask_blur: Represents the mask blur strength for blurring the depth map. This parameter controls the intensity of the depth map's blur treatment, used for preprocessing the depth map before calculating the final blur effect, to achieve a more natural blur transition.
"""
#OUTPUT_NODE = False
CATEGORY = "Pro Post/Blur Effects"
def depthblur_image(self, image: torch.Tensor, depth_map: torch.Tensor, blur_strength: float, focal_depth: float, focus_spread:float, steps: int, focal_range: float, mask_blur: int):
batch_size, height, width, _ = image.shape
image_result = torch.zeros_like(image)
mask_result = torch.zeros((batch_size, height, width), dtype=torch.float32)
for b in range(batch_size):
tensor_image = image[b].numpy()
tensor_image_depth = depth_map[b].numpy()
# Apply blur
blur_image,depth_mask = self.apply_depthblur(tensor_image, tensor_image_depth, blur_strength, focal_depth, focus_spread, steps, focal_range, mask_blur)
tensor_image = torch.from_numpy(blur_image).unsqueeze(0)
tensor_mask = torch.from_numpy(depth_mask).unsqueeze(0)
image_result[b] = tensor_image
mask_result[b] = tensor_mask
return (image_result,mask_result)
def apply_depthblur(self, image, depth_map, blur_strength, focal_depth, focus_spread, steps, focal_range, mask_blur):
# Normalize the input image if needed
needs_normalization = image.max() > 1
if needs_normalization:
image = image.astype(np.float32) / 255
# Normalize the depth map if needed
depth_map = depth_map.astype(np.float32) / 255 if depth_map.max() > 1 else depth_map
# Resize depth map to match the image dimensions
depth_map_resized = cv2.resize(depth_map, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_LINEAR)
if len(depth_map_resized.shape) > 2:
depth_map_resized = cv2.cvtColor(depth_map_resized, cv2.COLOR_BGR2GRAY)
# Adjust the depth map based on the focal plane
depth_mask = np.abs(depth_map_resized - focal_depth)
depth_mask = np.clip(depth_mask / np.max(depth_mask), 0, 1)
# Process the depth_mask
depth_mask[depth_mask < focal_range] = 0
depth_mask[depth_mask >= focal_range] = (depth_mask[depth_mask >= focal_range] - focal_range) / (1 - focal_range)
# Apply mask blur
depth_mask = cv2.GaussianBlur(depth_mask, (mask_blur, mask_blur), 0)
# Generate blurred versions of the image
blurred_images = processing_utils.generate_blurred_images(image, blur_strength, steps, focus_spread)
# Use the adjusted depth map as a mask for applying blurred images
final_image = processing_utils.apply_blurred_images(image, blurred_images, depth_mask)
# Convert back to original range if the image was normalized
if needs_normalization:
final_image = np.clip(final_image * 255, 0, 255).astype(np.uint8)
return final_image, depth_mask
class ProPostApplyLUT:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"lut_name": (folder_paths.get_filename_list("luts"), ),
"strength": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 1.0,
"step": 0.01
}),
"log": ("BOOLEAN", {
"default": False
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ()
FUNCTION = "lut_image"
#OUTPUT_NODE = False
CATEGORY = "Pro Post/Color Grading"
def lut_image(self, image: torch.Tensor, lut_name, strength: float, log: bool):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
# Read the LUT
lut_path = os.path.join(dir_luts, lut_name)
lut = loading_utils.read_lut(lut_path, clip=True)
for b in range(batch_size):
tensor_image = image[b].numpy()
# Apply LUT
lut_image = self.apply_lut(tensor_image, lut, strength, log)
tensor = torch.from_numpy(lut_image).unsqueeze(0)
result[b] = tensor
return (result,)
def apply_lut(self, image, lut, strength, log):
if strength == 0:
return image
# Apply the LUT
is_non_default_domain = not np.array_equal(lut.domain, np.array([[0., 0., 0.], [1., 1., 1.]]))
dom_scale = None
im_array = image.copy()
if is_non_default_domain:
dom_scale = lut.domain[1] - lut.domain[0]
im_array = im_array * dom_scale + lut.domain[0]
if log:
im_array = im_array ** (1/2.2)
im_array = lut.apply(im_array)
if log:
im_array = im_array ** (2.2)
if is_non_default_domain:
im_array = (im_array - lut.domain[0]) / dom_scale
# Blend the original image and the LUT-applied image based on the strength
blended_image = (1 - strength) * image + strength * im_array
return blended_image
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
"ProPostVignette": ProPostVignette,
"ProPostFilmGrain": ProPostFilmGrain,
"ProPostRadialBlur": ProPostRadialBlur,
"ProPostDepthMapBlur": ProPostDepthMapBlur,
"ProPostApplyLUT": ProPostApplyLUT
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"ProPostVignette": "ProPost Vignette",
"ProPostFilmGrain": "ProPost Film Grain",
"ProPostRadialBlur": "ProPost Radial Blur",
"ProPostDepthMapBlur": "ProPost Depth Map Blur",
"ProPostApplyLUT": "ProPost Apply LUT"
}