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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class SpriteFadeBatch50:
|
| 6 |
+
"""
|
| 7 |
+
Composites a foreground IMAGE over a background IMAGE and outputs a
|
| 8 |
+
50-frame IMAGE batch.
|
| 9 |
+
|
| 10 |
+
Fade schedule:
|
| 11 |
+
frame 1 = 100% foreground opacity
|
| 12 |
+
frame 2 = 96%
|
| 13 |
+
frame 3 = 92%
|
| 14 |
+
...
|
| 15 |
+
frame 26 = 0%
|
| 16 |
+
frame 27-50 = background only
|
| 17 |
+
|
| 18 |
+
Notes:
|
| 19 |
+
- Standard ComfyUI IMAGE tensors are RGB ([B,H,W,3]).
|
| 20 |
+
- If a non-standard 4-channel foreground arrives, the 4th channel is used as alpha.
|
| 21 |
+
- To preserve alpha from ComfyUI's built-in Load Image node, connect its MASK output
|
| 22 |
+
to the optional foreground_mask input. That MASK is inverted back to alpha.
|
| 23 |
+
- Only the first image from each incoming batch is used.
|
| 24 |
+
- The foreground is centered on the background and clipped if it extends beyond it.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
@classmethod
|
| 28 |
+
def INPUT_TYPES(cls):
|
| 29 |
+
return {
|
| 30 |
+
"required": {
|
| 31 |
+
"foreground": ("IMAGE",),
|
| 32 |
+
"background": ("IMAGE",),
|
| 33 |
+
},
|
| 34 |
+
"optional": {
|
| 35 |
+
"foreground_mask": ("MASK",),
|
| 36 |
+
},
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
RETURN_TYPES = ("IMAGE",)
|
| 40 |
+
RETURN_NAMES = ("images",)
|
| 41 |
+
FUNCTION = "build_batch"
|
| 42 |
+
CATEGORY = "image/compositing"
|
| 43 |
+
DESCRIPTION = "Fade a foreground sprite over a background and output a 50-image batch."
|
| 44 |
+
|
| 45 |
+
TOTAL_FRAMES = 50
|
| 46 |
+
OPACITY_STEP = 0.04 # 4%
|
| 47 |
+
|
| 48 |
+
def build_batch(self, foreground, background, foreground_mask=None):
|
| 49 |
+
fg = self._first_image(foreground)
|
| 50 |
+
bg = self._first_image(background)
|
| 51 |
+
|
| 52 |
+
fg_rgb, fg_alpha = self._extract_foreground_rgba(fg, foreground_mask)
|
| 53 |
+
bg_rgb = self._ensure_rgb(bg)
|
| 54 |
+
|
| 55 |
+
bg_h, bg_w = bg_rgb.shape[:2]
|
| 56 |
+
fg_h, fg_w = fg_rgb.shape[:2]
|
| 57 |
+
|
| 58 |
+
# Center the foreground on the background.
|
| 59 |
+
# Change these two lines if you want a different placement rule.
|
| 60 |
+
x_offset = (bg_w - fg_w) // 2
|
| 61 |
+
y_offset = (bg_h - fg_h) // 2
|
| 62 |
+
|
| 63 |
+
region = self._compute_overlap(
|
| 64 |
+
bg_h=bg_h,
|
| 65 |
+
bg_w=bg_w,
|
| 66 |
+
fg_h=fg_h,
|
| 67 |
+
fg_w=fg_w,
|
| 68 |
+
x_offset=x_offset,
|
| 69 |
+
y_offset=y_offset,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
frames = []
|
| 73 |
+
for frame_index in range(self.TOTAL_FRAMES):
|
| 74 |
+
opacity = max(1.0 - (self.OPACITY_STEP * frame_index), 0.0)
|
| 75 |
+
frame = bg_rgb.clone()
|
| 76 |
+
|
| 77 |
+
if region is not None and opacity > 0.0:
|
| 78 |
+
src_y1, src_y2, src_x1, src_x2, dst_y1, dst_y2, dst_x1, dst_x2 = region
|
| 79 |
+
|
| 80 |
+
src_rgb = fg_rgb[src_y1:src_y2, src_x1:src_x2, :]
|
| 81 |
+
src_alpha = fg_alpha[src_y1:src_y2, src_x1:src_x2].unsqueeze(-1) * opacity
|
| 82 |
+
dst_rgb = frame[dst_y1:dst_y2, dst_x1:dst_x2, :]
|
| 83 |
+
|
| 84 |
+
frame[dst_y1:dst_y2, dst_x1:dst_x2, :] = (
|
| 85 |
+
src_rgb * src_alpha + dst_rgb * (1.0 - src_alpha)
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
frames.append(frame)
|
| 89 |
+
|
| 90 |
+
output = torch.stack(frames, dim=0).clamp(0.0, 1.0)
|
| 91 |
+
return (output,)
|
| 92 |
+
|
| 93 |
+
def _first_image(self, image_tensor: torch.Tensor) -> torch.Tensor:
|
| 94 |
+
if image_tensor.ndim != 4:
|
| 95 |
+
raise ValueError(
|
| 96 |
+
f"Expected IMAGE tensor with shape [B,H,W,C], got shape {tuple(image_tensor.shape)}"
|
| 97 |
+
)
|
| 98 |
+
return image_tensor[0].to(torch.float32).clamp(0.0, 1.0)
|
| 99 |
+
|
| 100 |
+
def _ensure_rgb(self, image: torch.Tensor) -> torch.Tensor:
|
| 101 |
+
if image.ndim != 3:
|
| 102 |
+
raise ValueError(f"Expected image shape [H,W,C], got {tuple(image.shape)}")
|
| 103 |
+
|
| 104 |
+
channels = image.shape[-1]
|
| 105 |
+
if channels == 3:
|
| 106 |
+
return image
|
| 107 |
+
if channels >= 4:
|
| 108 |
+
return image[..., :3]
|
| 109 |
+
if channels == 1:
|
| 110 |
+
return image.repeat(1, 1, 3)
|
| 111 |
+
|
| 112 |
+
raise ValueError(f"Unsupported channel count: {channels}")
|
| 113 |
+
|
| 114 |
+
def _extract_foreground_rgba(self, foreground: torch.Tensor, foreground_mask=None):
|
| 115 |
+
channels = foreground.shape[-1]
|
| 116 |
+
|
| 117 |
+
if channels >= 4:
|
| 118 |
+
rgb = foreground[..., :3]
|
| 119 |
+
alpha = foreground[..., 3].clamp(0.0, 1.0)
|
| 120 |
+
else:
|
| 121 |
+
rgb = self._ensure_rgb(foreground)
|
| 122 |
+
alpha = torch.ones(
|
| 123 |
+
(foreground.shape[0], foreground.shape[1]),
|
| 124 |
+
dtype=foreground.dtype,
|
| 125 |
+
device=foreground.device,
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if foreground_mask is not None:
|
| 129 |
+
mask = self._normalize_mask(
|
| 130 |
+
foreground_mask,
|
| 131 |
+
target_h=rgb.shape[0],
|
| 132 |
+
target_w=rgb.shape[1],
|
| 133 |
+
dtype=rgb.dtype,
|
| 134 |
+
device=rgb.device,
|
| 135 |
+
)
|
| 136 |
+
# ComfyUI Load Image creates MASK from alpha and inverts it.
|
| 137 |
+
# Convert MASK back to alpha.
|
| 138 |
+
alpha = alpha * (1.0 - mask)
|
| 139 |
+
|
| 140 |
+
return rgb, alpha.clamp(0.0, 1.0)
|
| 141 |
+
|
| 142 |
+
def _normalize_mask(self, mask, target_h, target_w, dtype, device) -> torch.Tensor:
|
| 143 |
+
if isinstance(mask, torch.Tensor):
|
| 144 |
+
m = mask
|
| 145 |
+
else:
|
| 146 |
+
raise ValueError("foreground_mask must be a torch.Tensor")
|
| 147 |
+
|
| 148 |
+
# Accept common ComfyUI MASK shapes.
|
| 149 |
+
if m.ndim == 2: # [H,W]
|
| 150 |
+
m = m.unsqueeze(0).unsqueeze(0)
|
| 151 |
+
elif m.ndim == 3: # [B,H,W]
|
| 152 |
+
m = m[:1].unsqueeze(1)
|
| 153 |
+
elif m.ndim == 4: # [B,H,W,C] or [B,C,H,W]
|
| 154 |
+
if m.shape[-1] == 1:
|
| 155 |
+
m = m[:1].permute(0, 3, 1, 2)
|
| 156 |
+
elif m.shape[1] == 1:
|
| 157 |
+
m = m[:1]
|
| 158 |
+
else:
|
| 159 |
+
m = m[:1, :1]
|
| 160 |
+
else:
|
| 161 |
+
raise ValueError(f"Unsupported MASK shape: {tuple(m.shape)}")
|
| 162 |
+
|
| 163 |
+
m = m.to(device=device, dtype=dtype).clamp(0.0, 1.0)
|
| 164 |
+
|
| 165 |
+
if m.shape[-2:] != (target_h, target_w):
|
| 166 |
+
m = F.interpolate(m, size=(target_h, target_w), mode="bilinear", align_corners=False)
|
| 167 |
+
|
| 168 |
+
return m[0, 0]
|
| 169 |
+
|
| 170 |
+
def _compute_overlap(self, bg_h, bg_w, fg_h, fg_w, x_offset, y_offset):
|
| 171 |
+
dst_x1 = max(x_offset, 0)
|
| 172 |
+
dst_y1 = max(y_offset, 0)
|
| 173 |
+
dst_x2 = min(x_offset + fg_w, bg_w)
|
| 174 |
+
dst_y2 = min(y_offset + fg_h, bg_h)
|
| 175 |
+
|
| 176 |
+
if dst_x1 >= dst_x2 or dst_y1 >= dst_y2:
|
| 177 |
+
return None
|
| 178 |
+
|
| 179 |
+
src_x1 = max(-x_offset, 0)
|
| 180 |
+
src_y1 = max(-y_offset, 0)
|
| 181 |
+
src_x2 = src_x1 + (dst_x2 - dst_x1)
|
| 182 |
+
src_y2 = src_y1 + (dst_y2 - dst_y1)
|
| 183 |
+
|
| 184 |
+
return (src_y1, src_y2, src_x1, src_x2, dst_y1, dst_y2, dst_x1, dst_x2)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
NODE_CLASS_MAPPINGS = {
|
| 188 |
+
"SpriteFadeBatch50": SpriteFadeBatch50,
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 192 |
+
"SpriteFadeBatch50": "Sprite Fade Batch 50",
|
| 193 |
+
}
|