Skip to content

fix: support batch broadcasting in JoinImageWithAlpha node#13309

Open
Abdulrehman-PIAIC80387 wants to merge 1 commit intoComfy-Org:masterfrom
Abdulrehman-PIAIC80387:fix/join-image-alpha-batch-12580
Open

fix: support batch broadcasting in JoinImageWithAlpha node#13309
Abdulrehman-PIAIC80387 wants to merge 1 commit intoComfy-Org:masterfrom
Abdulrehman-PIAIC80387:fix/join-image-alpha-batch-12580

Conversation

@Abdulrehman-PIAIC80387
Copy link
Copy Markdown
Contributor

Summary

Fixes #12580

Problem

The Join Image with Alpha node only returns a single image when given a batch of images with a single alpha mask.

Root cause: In comfy_extras/nodes_compositing.py line 205, the batch size was calculated using min(len(image), len(alpha)). When a user passes a batch of 4 images with a single alpha mask, min(4, 1) = 1, so only one image is produced.

This is inconsistent with every other compositing node in ComfyUI (e.g., ImageCompositeMasked), which correctly broadcasts the shorter input to match the longer one.

Fix

Changed comfy_extras/nodes_compositing.py to use max() instead of min() for batch size, with modulo indexing (i % len(...)) to repeat the shorter input:

# Before
batch_size = min(len(image), len(alpha))
...
out_images.append(torch.cat((image[i][:,:,:3], alpha[i].unsqueeze(2)), dim=2))

# After
batch_size = max(len(image), len(alpha))
...
out_images.append(torch.cat((image[i % len(image)][:,:,:3], alpha[i % len(alpha)].unsqueeze(2)), dim=2))

This handles all cases correctly:

  • 4 images + 1 mask → 4 output images (mask repeats)
  • 1 image + 4 masks → 4 output images (image repeats)
  • 4 images + 4 masks → 4 output images (1-to-1)
  • 1 image + 1 mask → 1 output image (unchanged behavior)

…#12580)

The node used min(len(image), len(alpha)) as batch size, so passing
a batch of images with a single alpha mask only returned 1 image.

Changed to max() with modulo indexing to broadcast the shorter input,
matching the behavior of other compositing nodes like ImageCompositeMasked.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 6, 2026

📝 Walkthrough

Walkthrough

The JoinImageWithAlpha.execute method's batch processing logic has been modified. Instead of computing batch size using the minimum of image and alpha batch lengths, it now uses the maximum. Correspondingly, the per-item loop now uses modulo-based indexing (image[i % len(image)] and alpha[i % len(alpha)]) to wrap around smaller inputs, ensuring the output batch always matches the larger input's size through repetition. The alpha channel preprocessing and final concatenation operations remain unchanged.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly and concisely describes the main change: adding batch broadcasting support to the JoinImageWithAlpha node.
Description check ✅ Passed Description thoroughly explains the problem, root cause, fix implementation, and expected behavior with concrete examples.
Linked Issues check ✅ Passed Code changes directly implement the batch broadcasting fix required by issue #12580, using max() for batch size and modulo indexing for repetition.
Out of Scope Changes check ✅ Passed All changes are scoped to the JoinImageWithAlpha.execute method to implement batch broadcasting; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
comfy_extras/nodes_compositing.py (1)

205-210: Lock in the new broadcast behavior with a regression test.

This changes JoinImageWithAlpha from overlap-only batching to broadcast batching, so please add or update coverage for both N images + 1 mask and 1 image + N masks. tests-unit/comfy_extras_test/image_stitch_test.py:27-47 already shows the repo’s “larger batch wins” pattern for similar image ops.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@comfy_extras/nodes_compositing.py` around lines 205 - 210, JoinImageWithAlpha
now broadcasts batches (larger batch wins) so add a regression test that asserts
both broadcast cases: (a) N images + 1 mask and (b) 1 image + N masks produce
outputs with batch size equal to max(N_images, N_masks) and that the per-item
alpha channel matches the expected broadcasted mask values; locate the operator
under test (JoinImageWithAlpha in nodes_compositing.py, which uses resize_mask
and concatenates alpha) and follow the existing "larger batch wins" pattern from
the repo’s image-stitch tests to create two unit tests that construct small
synthetic tensors, run the operator, and assert output shape and channel content
for both scenarios.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@comfy_extras/nodes_compositing.py`:
- Around line 205-210: JoinImageWithAlpha now broadcasts batches (larger batch
wins) so add a regression test that asserts both broadcast cases: (a) N images +
1 mask and (b) 1 image + N masks produce outputs with batch size equal to
max(N_images, N_masks) and that the per-item alpha channel matches the expected
broadcasted mask values; locate the operator under test (JoinImageWithAlpha in
nodes_compositing.py, which uses resize_mask and concatenates alpha) and follow
the existing "larger batch wins" pattern from the repo’s image-stitch tests to
create two unit tests that construct small synthetic tensors, run the operator,
and assert output shape and channel content for both scenarios.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 18ced04d-feb6-46ff-9ad5-728129bdea09

📥 Commits

Reviewing files that changed from the base of the PR and between 4b1444f and bb5c337.

📒 Files selected for processing (1)
  • comfy_extras/nodes_compositing.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Join Image With Alpha node does not work with image batching

1 participant