File size: 2,263 Bytes
10a8e09 | 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 | # ComfyUI Custom Node: Remove (Exclude) Image @ Index from a Batch
# Save as:
# ComfyUI/custom_nodes/batch_remove_index/__init__.py
# Restart ComfyUI, then find it under category: "Batch/Index"
import torch
def _clamp_index(index: int, batch_size: int) -> int:
"""Clamp index into [0, batch_size-1]."""
if batch_size <= 0:
raise ValueError("Input batch is empty (batch_size <= 0).")
if index < 0 or index >= batch_size:
print(
f"[BatchRemoveIndex] index {index} out of range for batch_size {batch_size}; "
f"clamping to valid range."
)
index = max(0, min(index, batch_size - 1))
return index
class BatchRemoveImageAtIndex:
"""
Takes an IMAGE batch and an integer index (0-based),
outputs the batch WITHOUT the image at that index.
Example:
images = [img0,img1,img2,img3], index=2
output = [img0,img1,img3]
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"index": ("INT", {"default": 0, "min": 0, "max": 10**9}),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("images",)
FUNCTION = "remove"
CATEGORY = "Batch/Index"
def remove(self, images, index):
if not torch.is_tensor(images):
raise TypeError("Expected 'images' to be a torch Tensor (ComfyUI IMAGE type).")
if images.ndim != 4:
raise ValueError(f"Expected 'images' with shape [B,H,W,C], got ndim={images.ndim}.")
b = images.shape[0]
if b <= 1:
# Producing an empty IMAGE batch often breaks downstream nodes.
raise ValueError(
f"Cannot remove an item from a batch of size {b} (would produce an empty batch)."
)
idx = _clamp_index(int(index), b)
# Concatenate everything except the excluded index
out = torch.cat([images[:idx], images[idx + 1 :]], dim=0)
return (out,)
NODE_CLASS_MAPPINGS = {
"BatchRemoveImageAtIndex": BatchRemoveImageAtIndex,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"BatchRemoveImageAtIndex": "Batch: Remove Image @ Index",
} |