forked from Taremin/comfyui-string-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
283 lines (237 loc) · 9.1 KB
/
__init__.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
import random
from PIL import Image
import numpy as np
import torch
#
# === Старая нода: GlamRandomImage (без изменений) ===
#
class GlamRandomImage:
def __init__(self):
self.input_types = None
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"forceInput": True,
},
),
"image_1": ("IMAGE",),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process"
CATEGORY = "comfyui-glam-nodes"
def get_input_types(self):
if self.input_types is None:
self.input_types = self.INPUT_TYPES()
return self.input_types
def update_input_types(self, **kwargs):
input_types = self.get_input_types()
max_connected = 0
for i in range(1, 12):
key = f"image_{i}"
if key in kwargs and kwargs[key] is not None:
max_connected = i
if max_connected < 11:
next_slot = f"image_{max_connected + 1}"
input_types["required"][next_slot] = ("IMAGE",)
def process(self, *args, **kwargs):
seed = 0
if "seed" in kwargs:
seed = kwargs["seed"]
del kwargs["seed"]
images = []
for i in range(1, 12):
key = f"image_{i}"
if key in kwargs and kwargs[key] is not None:
images.append(kwargs[key])
if not images:
raise ValueError("At least one image input is required")
random.seed(seed)
choice = random.choice(images)
self.update_input_types(**kwargs)
return (choice,)
#
# === GlamSmoothZoom (Zoom In, без LANCZOS) ===
#
class GlamSmoothZoom:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
"""
zoom_factor: на сколько увеличить (напр. 0.15 = +15%)
duration: длительность анимации (сек)
fps: кадров в секунду
interpolation: ["BICUBIC","BILINEAR"] (убрали LANCZOS)
easing: тип плавности
"""
return {
"required": {
"image": ("IMAGE",),
"zoom_factor": ("FLOAT", {"default": 0.15, "min": 0.0, "max": 1.0, "step": 0.01}),
"duration": ("FLOAT", {"default": 5.0, "min": 0.1, "max": 60.0, "step": 0.1}),
"fps": ("INT", {"default": 30, "min": 1, "max": 240, "step": 1}),
"interpolation": (["BICUBIC", "BILINEAR"], {"default": "BICUBIC"}),
"easing": (["linear", "ease_in_out", "ease_out"], {"default": "ease_in_out"}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process"
CATEGORY = "comfyui-glam-nodes"
def ease_in_out(self, t):
return t * t * (3 - 2 * t)
def ease_out(self, t):
return 1 - (1 - t) ** 3
def process(self, image, zoom_factor, duration, fps, interpolation, easing):
"""
"Zoom In" (увеличение) без чёрных рамок.
Масштаб растёт от 1.0 до 1.0+zoom_factor.
"""
# 1) PyTorch->NumPy
if isinstance(image, torch.Tensor):
frame_0 = image[0].detach().cpu().numpy()
else:
frame_0 = image[0]
frame_0 = (frame_0 * 255).astype(np.uint8)
pil_image = Image.fromarray(frame_0)
width, height = pil_image.size
total_frames = int(fps * duration)
def apply_easing(raw_t):
if easing == "ease_in_out":
return self.ease_in_out(raw_t)
elif easing == "ease_out":
return self.ease_out(raw_t)
return raw_t # linear
from PIL import Image as PILImage
if interpolation == "BICUBIC":
resample_method = PILImage.Resampling.BICUBIC
else:
resample_method = PILImage.Resampling.BILINEAR
cx = width / 2.0
cy = height / 2.0
frames_list = []
for i in range(total_frames):
t = i / (total_frames - 1) if total_frames > 1 else 0.0
t = apply_easing(t)
# Zoom In: scale = 1..(1+zoom_factor)
final_scale = 1.0 + zoom_factor * t
# В transform() указываем 1/final_scale, чтобы обрезать края
a = 1.0 / final_scale
b = 0.0
c = cx * (1.0 - a)
d = 0.0
e = 1.0 / final_scale
f = cy * (1.0 - e)
coeffs = (a, b, c, d, e, f)
transformed = pil_image.transform(
(width, height),
PILImage.AFFINE,
coeffs,
resample=resample_method
)
frame_array = np.array(transformed, dtype=np.float32) / 255.0
frames_list.append(frame_array)
frames_np = np.stack(frames_list, axis=0)
frames_torch = torch.from_numpy(frames_np)
return (frames_torch,)
#
# === Новая нода: GlamSmoothZoomOut (Zoom Out) ===
#
class GlamSmoothZoomOut:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
"""
По умолчанию 5с, 30 fps, zoom_factor говорит,
насколько изначально изображение больше, чем 1.0.
"""
return {
"required": {
"image": ("IMAGE",),
"zoom_factor": ("FLOAT", {"default": 0.15, "min": 0.0, "max": 1.0, "step": 0.01}),
"duration": ("FLOAT", {"default": 5.0, "min": 0.1, "max": 60.0, "step": 0.1}),
"fps": ("INT", {"default": 30, "min": 1, "max": 240, "step": 1}),
"interpolation": (["BICUBIC", "BILINEAR"], {"default": "BICUBIC"}),
"easing": (["linear", "ease_in_out", "ease_out"], {"default": "ease_in_out"}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process"
CATEGORY = "comfyui-glam-nodes"
def ease_in_out(self, t):
return t * t * (3 - 2 * t)
def ease_out(self, t):
return 1 - (1 - t) ** 3
def process(self, image, zoom_factor, duration, fps, interpolation, easing):
"""
"Zoom Out": в начале масштаб (1.0 + zoom_factor), а к концу 1.0.
Также без чёрных рамок, анимируем через матрицу aффинного transform.
"""
# Подготовка
if isinstance(image, torch.Tensor):
frame_0 = image[0].detach().cpu().numpy()
else:
frame_0 = image[0]
frame_0 = (frame_0 * 255).astype(np.uint8)
pil_image = Image.fromarray(frame_0)
width, height = pil_image.size
total_frames = int(fps * duration)
def apply_easing(raw_t):
if easing == "ease_in_out":
return self.ease_in_out(raw_t)
elif easing == "ease_out":
return self.ease_out(raw_t)
return raw_t # linear
from PIL import Image as PILImage
if interpolation == "BICUBIC":
resample_method = PILImage.Resampling.BICUBIC
else:
resample_method = PILImage.Resampling.BILINEAR
# Центр
cx = width / 2.0
cy = height / 2.0
frames_list = []
for i in range(total_frames):
t = i / (total_frames - 1) if total_frames > 1 else 0.0
t = apply_easing(t)
# Zoom Out: scale(t=0) = 1 + zoom_factor, scale(t=1) = 1.0
# => final_scale = (1.0 + zoom_factor) - zoom_factor*t
# или 1.0 + zoom_factor*(1 - t)
final_scale = 1.0 + zoom_factor * (1.0 - t)
# Для transform: a = 1/final_scale
# => "к концу" a становится = 1/1=1
a = 1.0 / final_scale
b = 0.0
c = cx * (1.0 - a)
d = 0.0
e = 1.0 / final_scale
f = cy * (1.0 - e)
coeffs = (a, b, c, d, e, f)
transformed = pil_image.transform(
(width, height),
PILImage.AFFINE,
coeffs,
resample=resample_method
)
frame_array = np.array(transformed, dtype=np.float32) / 255.0
frames_list.append(frame_array)
frames_np = np.stack(frames_list, axis=0)
frames_torch = torch.from_numpy(frames_np)
return (frames_torch,)
#
# === Обновляем словарь нод ===
#
NODE_CLASS_MAPPINGS = {
"GlamRandomImage": GlamRandomImage,
"GlamSmoothZoom": GlamSmoothZoom,
"GlamSmoothZoomOut": GlamSmoothZoomOut,
}
WEB_DIRECTORY = "./js"