Upload Batch_Img_Remove_At_13.py
Browse files- Batch_Img_Remove_At_13.py +132 -0
Batch_Img_Remove_At_13.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ComfyUI Custom Node: Remove up to 13 images from a batch "at once"
|
| 2 |
+
# - Takes a batch (IMAGE)
|
| 3 |
+
# - Takes 13 index inputs
|
| 4 |
+
# - Removes all specified indices simultaneously (no shifting issues)
|
| 5 |
+
# - Index = -1 means "do not remove anything" (ignored)
|
| 6 |
+
#
|
| 7 |
+
# Save as:
|
| 8 |
+
# ComfyUI/custom_nodes/batch_remove_13_indices/__init__.py
|
| 9 |
+
# Restart ComfyUI, then find under: "Batch/Index"
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class BatchRemoveImagesAt13Indices:
|
| 15 |
+
"""
|
| 16 |
+
Remove multiple images from a batch at once.
|
| 17 |
+
|
| 18 |
+
Notes:
|
| 19 |
+
- Indices are 0-based.
|
| 20 |
+
- Any index == -1 is ignored (meaning: remove nothing for that slot).
|
| 21 |
+
- Out-of-range indices are ignored (with a console warning).
|
| 22 |
+
- Duplicate indices are fine (removed once).
|
| 23 |
+
- If removal would produce an empty batch, this node raises an error.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
@classmethod
|
| 27 |
+
def INPUT_TYPES(cls):
|
| 28 |
+
# 13 separate INT inputs, defaulting to -1
|
| 29 |
+
idx_cfg = {"default": -1, "min": -1, "max": 10**9}
|
| 30 |
+
return {
|
| 31 |
+
"required": {
|
| 32 |
+
"images": ("IMAGE",),
|
| 33 |
+
|
| 34 |
+
"remove_index_01": ("INT", idx_cfg),
|
| 35 |
+
"remove_index_02": ("INT", idx_cfg),
|
| 36 |
+
"remove_index_03": ("INT", idx_cfg),
|
| 37 |
+
"remove_index_04": ("INT", idx_cfg),
|
| 38 |
+
"remove_index_05": ("INT", idx_cfg),
|
| 39 |
+
"remove_index_06": ("INT", idx_cfg),
|
| 40 |
+
"remove_index_07": ("INT", idx_cfg),
|
| 41 |
+
"remove_index_08": ("INT", idx_cfg),
|
| 42 |
+
"remove_index_09": ("INT", idx_cfg),
|
| 43 |
+
"remove_index_10": ("INT", idx_cfg),
|
| 44 |
+
"remove_index_11": ("INT", idx_cfg),
|
| 45 |
+
"remove_index_12": ("INT", idx_cfg),
|
| 46 |
+
"remove_index_13": ("INT", idx_cfg),
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
RETURN_TYPES = ("IMAGE",)
|
| 51 |
+
RETURN_NAMES = ("images",)
|
| 52 |
+
FUNCTION = "remove"
|
| 53 |
+
CATEGORY = "Batch/Index"
|
| 54 |
+
|
| 55 |
+
def remove(
|
| 56 |
+
self,
|
| 57 |
+
images,
|
| 58 |
+
remove_index_01,
|
| 59 |
+
remove_index_02,
|
| 60 |
+
remove_index_03,
|
| 61 |
+
remove_index_04,
|
| 62 |
+
remove_index_05,
|
| 63 |
+
remove_index_06,
|
| 64 |
+
remove_index_07,
|
| 65 |
+
remove_index_08,
|
| 66 |
+
remove_index_09,
|
| 67 |
+
remove_index_10,
|
| 68 |
+
remove_index_11,
|
| 69 |
+
remove_index_12,
|
| 70 |
+
remove_index_13,
|
| 71 |
+
):
|
| 72 |
+
if not torch.is_tensor(images):
|
| 73 |
+
raise TypeError("Expected 'images' to be a torch Tensor (ComfyUI IMAGE type).")
|
| 74 |
+
if images.ndim != 4:
|
| 75 |
+
raise ValueError(f"Expected 'images' with shape [B,H,W,C], got ndim={images.ndim}.")
|
| 76 |
+
|
| 77 |
+
b = int(images.shape[0])
|
| 78 |
+
if b <= 0:
|
| 79 |
+
raise ValueError("Input batch is empty.")
|
| 80 |
+
|
| 81 |
+
raw_indices = [
|
| 82 |
+
remove_index_01, remove_index_02, remove_index_03, remove_index_04, remove_index_05,
|
| 83 |
+
remove_index_06, remove_index_07, remove_index_08, remove_index_09, remove_index_10,
|
| 84 |
+
remove_index_11, remove_index_12, remove_index_13,
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
# Build a set of indices to remove (simultaneous removal)
|
| 88 |
+
remove_set = set()
|
| 89 |
+
for idx in raw_indices:
|
| 90 |
+
idx = int(idx)
|
| 91 |
+
|
| 92 |
+
# Sentinel: -1 means "do not remove"
|
| 93 |
+
if idx == -1:
|
| 94 |
+
continue
|
| 95 |
+
|
| 96 |
+
# Disallow other negative indices (since -1 is reserved)
|
| 97 |
+
if idx < -1:
|
| 98 |
+
print(f"[BatchRemove13] Ignoring invalid negative index {idx} (only -1 is allowed).")
|
| 99 |
+
continue
|
| 100 |
+
|
| 101 |
+
# Ignore out-of-range indices rather than clamping (clamping could remove the wrong image)
|
| 102 |
+
if idx < 0 or idx >= b:
|
| 103 |
+
print(f"[BatchRemove13] Ignoring out-of-range index {idx} for batch size {b}.")
|
| 104 |
+
continue
|
| 105 |
+
|
| 106 |
+
remove_set.add(idx)
|
| 107 |
+
|
| 108 |
+
# If nothing to remove, return original batch unchanged
|
| 109 |
+
if not remove_set:
|
| 110 |
+
return (images,)
|
| 111 |
+
|
| 112 |
+
if len(remove_set) >= b:
|
| 113 |
+
raise ValueError(
|
| 114 |
+
f"Removal would produce an empty batch (batch size {b}, requested removals {len(remove_set)})."
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Keep mask (True = keep, False = remove)
|
| 118 |
+
keep_mask = torch.ones((b,), dtype=torch.bool, device=images.device)
|
| 119 |
+
remove_idx = torch.tensor(sorted(remove_set), dtype=torch.long, device=images.device)
|
| 120 |
+
keep_mask[remove_idx] = False
|
| 121 |
+
|
| 122 |
+
out = images[keep_mask]
|
| 123 |
+
return (out,)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
NODE_CLASS_MAPPINGS = {
|
| 127 |
+
"BatchRemoveImagesAt13Indices": BatchRemoveImagesAt13Indices,
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 131 |
+
"BatchRemoveImagesAt13Indices": "Batch: Remove 13 Indices (At Once)",
|
| 132 |
+
}
|