Upload salia_Get_First_and_Last_Batch.py
Browse files
salia_Get_First_and_Last_Batch.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Save this as: ComfyUI/custom_nodes/first_last_image.py
|
| 2 |
+
# Restart ComfyUI after saving.
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class FirstLastImageFromBatch:
|
| 8 |
+
"""
|
| 9 |
+
Takes a batch of ComfyUI IMAGE tensors and outputs:
|
| 10 |
+
- first image (as a batch of size 1)
|
| 11 |
+
- last image (as a batch of size 1)
|
| 12 |
+
|
| 13 |
+
ComfyUI IMAGE is typically a torch.Tensor with shape: [B, H, W, C]
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
CATEGORY = "image/batch"
|
| 17 |
+
FUNCTION = "first_last"
|
| 18 |
+
RETURN_TYPES = ("IMAGE", "IMAGE")
|
| 19 |
+
RETURN_NAMES = ("first", "last")
|
| 20 |
+
|
| 21 |
+
@classmethod
|
| 22 |
+
def INPUT_TYPES(cls):
|
| 23 |
+
return {"required": {"images": ("IMAGE",)}}
|
| 24 |
+
|
| 25 |
+
def first_last(self, images):
|
| 26 |
+
if not isinstance(images, torch.Tensor):
|
| 27 |
+
raise TypeError(f"Expected IMAGE as torch.Tensor, got {type(images)}")
|
| 28 |
+
|
| 29 |
+
# Accept single image [H, W, C] as batch size 1
|
| 30 |
+
if images.dim() == 3:
|
| 31 |
+
images = images.unsqueeze(0)
|
| 32 |
+
elif images.dim() != 4:
|
| 33 |
+
raise ValueError(f"Expected images with 3 or 4 dims, got shape {tuple(images.shape)}")
|
| 34 |
+
|
| 35 |
+
b = images.shape[0]
|
| 36 |
+
if b < 1:
|
| 37 |
+
raise ValueError("Received an empty image batch (B=0).")
|
| 38 |
+
|
| 39 |
+
# Slice to keep batch dimension, return batches of size 1
|
| 40 |
+
first = images[0:1]
|
| 41 |
+
last = images[-1:]
|
| 42 |
+
|
| 43 |
+
# Optional: clone to avoid any accidental in-place edits elsewhere affecting outputs
|
| 44 |
+
return (first.clone(), last.clone())
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
NODE_CLASS_MAPPINGS = {
|
| 48 |
+
"FirstLastImageFromBatch": FirstLastImageFromBatch,
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 52 |
+
"FirstLastImageFromBatch": "First + Last Image (from batch)",
|
| 53 |
+
}
|