Nodes/ComfyUI-UniversalToolkit/Get Image or Mask Range From Batch (UTK)
ComfyUI Node

Get Image or Mask Range From Batch (UTK)

Slice a range out of an image or mask batch without touching a tensor

By whmc76·Created about a year ago·Updated 2 months ago· 72
Get Image or Mask Range From Batch (UTK)
  • images
  • masks
  • image
  • mask
start_index0
num_frames1

Every video or animation workflow eventually hits the same wall: your batch has 120 frames and you only want frames 40 through 50, or you want the very last frame to use as an img2img seed. The built-in answers are awkward - you can grab a single image with an Index node, but grabbing a range usually means a custom Python node or a lot of fiddling. Get Image or Mask Range From Batch (UTK) is that missing tool, from the ComfyUI-UniversalToolkit pack.

It's gloriously simple, which is why it's worth a slot in your toolbox: two integers in, one slice out, no torch in sight.

How it works

Under the hood it's a plain tensor slice: images[start_index : start_index + num_frames], with bounds clamped so it never throws on the tail. The start_index special value is -1, which means "start from the end" - that's how you grab the last num_frames without counting. Handy for pulling the final frame of a video batch for an end-card or a loop point.

You can pass images, masks, or both. If you feed both, you get both outputs sliced with the same indices, which keeps image/mask pairs aligned - exactly what you want when you're taking a sub-range of a batch into a masked pipeline and don't want the mask and image to drift apart.

Inputs and outputs

  • start_index (INT, default 0, max 4096) - first frame to take; -1 means "from the end."
  • num_frames (INT, default 1) - how many frames to grab.
  • images / masks (optional) - the batch(es) to slice. At least one is required; the node raises a clear error if you feed neither.

Outputs: image and mask, each matching whatever input you gave it. If you only fed images, mask comes back as None - don't wire it into something that expects a real mask.

Install and gotchas

Install via ComfyUI Manager (search "ComfyUI-UniversalToolkit") or:

cd ComfyUI/custom_nodes
git clone https://github.com/whmc76/ComfyUI-UniversalToolkit
pip install -r requirements.txt

Restart ComfyUI after. This node pulls in torch and numpy - you already have both, so it's about as dependency-free as a custom node gets. No models, no keys.

Two things to keep in mind. First, start_index can't be negative except the special -1; -2 won't do what you hope. Second, if start_index is beyond the batch, it raises a ValueError instead of silently returning empty - which is honestly the right behavior, because a silent empty batch is how you end up with a black frame you stare at for an hour. For most people the use case is boring and that's the point: feed it a video's frames, slice the range you care about, run a detail pass on just those frames instead of the whole clip.

CategoryUniversalToolkit/Tools

Inputs (4)

NameTypeDefaultDescription
start_indexINT0-1–4096起始索引,-1表示从末尾开始
num_framesINT11–4096要提取的帧数
imagesoptIMAGE输入的图像批次
masksoptMASK输入的遮罩批次

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK