Nodes/comfyui_my_img_util/Auto Image Selector
ComfyUI Node

Auto Image Selector

The fallback switch that uses numbers, not wires

By sugarkwork·Created about a year ago·Updated 7 months ago· 2
Auto Image Selector
  • image1
  • image2
  • image3
  • image4
  • ng_image
  • image
  • rank
rank11
rank22
rank33
rank44

ComfyUI has no native "give me whichever of these images is the best one right now" node. There are switches - rgthree's Any Switch walks its inputs and returns the first non-empty one - but that's a fixed wire order with no numbers attached. Auto Image Selector is the numbered version: you give each of up to four images a rank, and it outputs the connected image with the lowest rank. Lower wins. It's the plumbing-layer answer to "my workflow has several possible image sources and I don't want to rewire it by hand every time."

How it works

The mechanism is one loop over four image/rank pairs. An entry is "valid" if an image is actually connected and its rank is at least zero. The node tracks the smallest rank it's seen, returns that image, and reports the winning rank. Ties go to the first input. There's also an ng_image input that blacklists anything exactly equal to it.

That ng_image bit is where people get burned, so let's be precise: "exactly equal" means torch.equal - identical tensor shape, values, and dtype. It only excludes the literal same tensor flowing through that wire. Re-encode a copy, resize it, or pass it through any node, and it's a different tensor that won't match. Treat ng_image as "exclude this exact upstream branch", not "exclude images that look like this". The second behavior needs an actual image-comparison node, and this isn't it.

The inputs and outputs that matter

Everything is optional, which is the whole trick:

  • image1image4 - candidate IMAGEs. Leave any unwired; they get skipped.
  • rank1rank4 - INT priority, defaults 1, 2, 3, 4, minimum 1. Smaller number = higher priority.
  • ng_image - an IMAGE; any input exactly equal to it is skipped.

Outputs:

  • image - the winning IMAGE.
  • rank - the INT rank that won, handy when you want downstream logic to know why it won.

The one setting you'll actually touch is the ranks. By default image1 wins, because its rank is 1. Want image2 to win when it's connected? Set rank2 to 1. You can also right-click a rank widget → Convert widget to input, and let another node decide priority at runtime. That's when this stops being a static switch and becomes actual logic.

Installing it

Same pack, same story as the rest of comfyui_my_img_util. ComfyUI Manager, search "comfyui_my_img_util", or:

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

Restart, and it's under the "image" category. Dependencies are numpy, Pillow, and opencv-python; no models, no downloads. One pack-level caveat: nodes.py imports cv2 at module load, so a missing opencv-python breaks every node in the pack, not just the denoise one. Don't skip the pip install.

Common issues

  • Nothing connects and the graph breaks downstream. If no input is valid, the node returns None for the image and sys.maxsize - a gigantic integer - for the rank. None flowing into a node that expects a tensor is a hard error, and the message won't point back here. The fix is structural: keep at least one branch always wired, or feed the output into a fallback that tolerates None.
  • The ng_image blacklist "doesn't work". Feeding in a slightly different image and expecting exclusion is the exact-equality behavior above, not a bug.
  • Rank 0 vs. min 1. The UI says the minimum is 1, but the code accepts 0 (the author's own test uses it). Not a problem unless you hoped rank 0 meant "disabled" - it doesn't.

Where you'd actually use it

Any graph where the "best" image source changes run to run: a ControlNet workflow that takes a pose reference or a depth map depending on what's available, a batch process that wants a different reference when one is absent, optional branches feeding a single consumer. It's a small, honest node - one loop, one comparison - and for that narrow "lowest rank wins" job it's exactly what you want.

Categoryimage

Inputs (9)

NameTypeDefaultDescription
image1optIMAGE
rank1optINT1
image2optIMAGE
rank2optINT2
image3optIMAGE
rank3optINT3
image4optIMAGE
rank4optINT4
ng_imageoptIMAGE

Outputs (2)

NameTypeDescription
imageIMAGE
rankINT