Upload 2 files
Browse files- Batch_Remove_First_Last.py +59 -0
Batch_Remove_First_Last.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Save as: ComfyUI/custom_nodes/batch_remove_first_last.py
|
| 2 |
+
# Restart ComfyUI after saving.
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BatchRemoveFirstLast:
|
| 8 |
+
"""
|
| 9 |
+
Takes an IMAGE batch and returns the same batch, except:
|
| 10 |
+
- removes the FIRST image (index 0)
|
| 11 |
+
- removes the LAST image (index B-1)
|
| 12 |
+
|
| 13 |
+
Output = images[1:-1]
|
| 14 |
+
|
| 15 |
+
Notes:
|
| 16 |
+
- If the batch has fewer than 3 images (B < 3), removing both ends would
|
| 17 |
+
produce an empty/invalid batch, so this node returns the original batch.
|
| 18 |
+
- If a single image comes in as [H, W, C], it is treated as a batch of 1.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
CATEGORY = "image/batch"
|
| 22 |
+
FUNCTION = "remove_first_last"
|
| 23 |
+
|
| 24 |
+
RETURN_TYPES = ("IMAGE",)
|
| 25 |
+
RETURN_NAMES = ("images",)
|
| 26 |
+
|
| 27 |
+
@classmethod
|
| 28 |
+
def INPUT_TYPES(cls):
|
| 29 |
+
return {"required": {"images": ("IMAGE",)}}
|
| 30 |
+
|
| 31 |
+
def remove_first_last(self, images):
|
| 32 |
+
if not isinstance(images, torch.Tensor):
|
| 33 |
+
# Best-effort fallback
|
| 34 |
+
return (images,)
|
| 35 |
+
|
| 36 |
+
# Normalize to [B, H, W, C]
|
| 37 |
+
if images.dim() == 3:
|
| 38 |
+
images = images.unsqueeze(0)
|
| 39 |
+
elif images.dim() != 4:
|
| 40 |
+
# Unknown shape; return unchanged
|
| 41 |
+
return (images,)
|
| 42 |
+
|
| 43 |
+
b = int(images.shape[0])
|
| 44 |
+
|
| 45 |
+
# If too small to safely remove both ends, return original batch
|
| 46 |
+
if b < 3:
|
| 47 |
+
return (images,)
|
| 48 |
+
|
| 49 |
+
out = images[1:-1].clone()
|
| 50 |
+
return (out,)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
NODE_CLASS_MAPPINGS = {
|
| 54 |
+
"BatchRemoveFirstLast": BatchRemoveFirstLast,
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 58 |
+
"BatchRemoveFirstLast": "Batch Remove First + Last",
|
| 59 |
+
}
|