Nodes/ComfyUI-QING/批处理丨逐项提取
ComfyUI Node

批处理丨逐项提取

Pull one image out of a batch at a time, with a loop counter

By sheengoa·Created about a year ago·Updated 20 days ago· 16
批处理丨逐项提取
  • images
  • item_image
  • total
  • is_last
index0
loop_mode截断

A ComfyUI image "batch" is just frames stacked on dimension 0, and sometimes you want to process them one at a time instead of all at once. QING_BatchForEach is the extractor half of that: give it a batch, tell it which index you want, and it hands you a single image - plus the batch size and a "this is the last one" flag so you can build a real loop around it.

What it does

Inputs are images (the batch), index (int, default 0, and it accepts negative values), and loop_mode - the out-of-range strategy, with three options:

  • 截断 (clamp, default) - out-of-range indexes clamp to the nearest valid one.
  • 环绕 (wrap) - modulo arithmetic, so index 5 on a batch of 3 gives you index 2. Negatives wrap too.
  • 报错 (error) - out-of-range indexes raise a clear error instead of guessing.

Outputs: item_image (the single extracted frame), total (batch size, so the loop knows when it's done), and is_last (boolean - true when the index hits the final frame).

The mechanism is dead simple under the hood: images[actual_index:actual_index+1] keeps the frame's batch dimension so it still behaves like a 1-frame batch downstream. is_last is just index == total - 1.

The loop it's built for

This is the "for each" side of a batch loop, and its natural partner is the pack's QING_BatchCollect - you iterate a batch through a per-image process (an inpaint pass, a quality check, an API call) and collect the results back into a batch. A simple iteration looks like:

  • QING_BatchForEach extracts frame N.
  • Process the single frame (detailer, quality score, whatever).
  • Feed the result to QING_BatchCollect (with reset on for the first pass to clear stale cache).
  • A counter or loop node bumps index, and is_last tells the loop when to stop.

Because each frame gets its own pass, you can apply per-image logic - a conditional that would be awkward across a whole batch at once. That's the "graph as a small program" pattern from the KB's node-plumbing doc, and this is the index-extraction primitive it's built on.

Installing it

Part of ComfyUI-QING:

cd ComfyUI/custom_nodes
git clone https://github.com/GAO-SHIQING/ComfyUI-QING
cd ComfyUI-QING
python install_dependencies.py

Restart ComfyUI after. Pure torch code, no extra dependencies beyond what the pack's installer already handles. (If you use ComfyUI Manager, search "ComfyUI-QING" and it's the same thing. Ignore the README's GAOSHI-QING clone-URL typo - the repo is GAO-SHIQING/ComfyUI-QING.)

Things to know

The empty-batch case raises an explicit error (images is empty) rather than returning garbage, so guard upstream if a batch can legitimately be zero-length. With clamp mode you can't actually "run past" the end - the last frame keeps repeating while the index climbs, which is why the is_last flag exists; use it to stop your loop, don't rely on the index erroring. And remember the output keeps the batch dimension: item_image is a 1-frame batch, so it plugs straight back into batch-oriented nodes without reshaping.

CategoryQING/数据工具

Inputs (3)

NameTypeDefaultDescription
imagesIMAGE输入图像批次
indexINT0-1000000–1000000要提取的索引
loop_modeCOMBO截断索引越界处理策略

Outputs (3)

NameTypeDescription
item_imageIMAGE
totalINT
is_lastBOOLEAN