ComfyUI/tests-unit/comfy_extras_test/image_blend_workflow_test.py
Glary-Bot ae88cd1966 Cap ImageBlend channel-mismatch output at 4 channels (RGBA)
Address review feedback: the previous fix allowed ImageBlend to return
tensors with > 4 channels (e.g. blending a 3-channel and a 5-channel
image produced a 5-channel tensor). This shifted the original failure
from blend-time to save/preview-time, because SaveImage and PreviewImage
both call PIL.Image.fromarray, which only supports 1/3/4-channel arrays.

Fix:
- In Blend.execute, the alignment target is now min(max(c1, c2), 4):
  any image with more than 4 channels is truncated, any image with
  fewer is padded with 1.0s up to the (capped) target. This makes the
  RGB/RGBA case work and also makes the >4-channel case work end-to-end
  rather than just deferring its failure.
- Update the regression test that previously codified the wrong
  5-channel-output behavior to assert the correct 4-channel cap.
- Add test_output_capped_at_four_channels (both inputs > 4 channels).
- Add test_save_compatible_output_passes_through_pil that mirrors
  SaveImage's exact PIL.Image.fromarray conversion to catch regressions
  in the save/preview path.
- Add a small workflow-validation test (image_blend_workflow_test.py)
  that loads tests/inference/graphs/image_blend_channel_mismatch.json
  and verifies its node types and wiring, so the demo workflow can't
  silently bitrot.

Verified end-to-end against a local ComfyUI server: the workflow runs,
output is RGBA, downstream SaveImage succeeds.
2026-04-27 07:18:16 +00:00

57 lines
1.8 KiB
Python

import json
import pathlib
WORKFLOW_PATH = (
pathlib.Path(__file__).resolve().parents[2]
/ "tests"
/ "inference"
/ "graphs"
/ "image_blend_channel_mismatch.json"
)
def test_workflow_loads():
with open(WORKFLOW_PATH) as f:
graph = json.load(f)
assert isinstance(graph, dict) and graph, "workflow JSON is empty"
def test_workflow_uses_expected_node_types():
"""The workflow uses a fixed, minimal set of nodes. If any are renamed
or removed upstream, this test fails fast instead of letting the demo
bitrot silently."""
expected = {
"EmptyImage",
"SolidMask",
"JoinImageWithAlpha",
"ImageBlend",
"SaveImage",
}
with open(WORKFLOW_PATH) as f:
graph = json.load(f)
actual = {node["class_type"] for node in graph.values()}
assert expected.issubset(actual), (
f"workflow is missing required node types: {expected - actual}"
)
def test_workflow_exercises_imageblend_with_mismatched_channels():
"""Sanity-check that the workflow actually wires an RGB output and an
RGBA output into ImageBlend (the CORE-103 case). If someone edits the
JSON and accidentally breaks this guarantee, the demo loses its point."""
with open(WORKFLOW_PATH) as f:
graph = json.load(f)
blend_nodes = [n for n in graph.values() if n["class_type"] == "ImageBlend"]
assert len(blend_nodes) == 1, "expected exactly one ImageBlend node"
blend = blend_nodes[0]
src1_id, _ = blend["inputs"]["image1"]
src2_id, _ = blend["inputs"]["image2"]
types = {graph[src1_id]["class_type"], graph[src2_id]["class_type"]}
assert "JoinImageWithAlpha" in types, (
"workflow no longer feeds an RGBA image into ImageBlend"
)
assert "EmptyImage" in types, (
"workflow no longer feeds a plain RGB image into ImageBlend"
)