Nodes/ComfyUI Layer Style/LayerUtility: IC Mask
ComfyUI Node Runs on cloud

LayerUtility: IC Mask

IC Mask — stitch a reference image into one canvas for in-context editing

By chflame163·Created 3 years ago·Updated 4 days ago· 3,113
LayerUtility: IC Mask
  • first_image
  • first_mask
  • second_image
  • second_mask
  • image
  • mask
  • icmask_data
â—„patch_modeautoâ–º
â—„output_length1536â–º
â—„patch_color#FFFFFFâ–º

"IC" here means in-context, and this node builds the input canvas that the whole in-context editing trick depends on. The idea, which has become the default way people get modern edit models (Qwen-Image-Edit, Flux Kontext and friends) to do faithful, reference-driven edits: instead of asking the model to imagine something from a text prompt alone, you glue your reference image and a blank patch region together into one combined canvas, hand the whole thing to the edit model, and let it fill the patch using the reference as visual context sitting right next to it. IC Mask is the node that builds that combined canvas and remembers exactly where the patch was, so a companion node can pull the result back out afterward.

This is a close cousin of the masked "Inpaint Crop → edit model → Stitch" pattern that's become standard for getting precise, non-drifting edits out of instruction models - same underlying instinct (don't let the model touch pixels you don't need it to touch), different mechanism: side-by-side stitching instead of a paint mask.

How it works

You give it a first_image (and optionally a first_mask), and optionally a second_image/second_mask. It arranges these into one canvas sized by output_length, placing the patch region either to the right or below the first image - patch_mode controls this, with auto picking a sensible layout based on aspect ratio, or you can force patch_right/patch_bottom. Wherever there's no second image to place, the patch area gets filled with a flat, easy-to-target patch_color. The output bundles the composite image, a mask marking the patch region for the edit model, and icmask_data - a record of exactly where that patch sits - which its partner node, IC Mask Crop Back, needs to extract just the new content afterward.

The inputs and outputs that matter

  • first_image (required) - your base or reference image.
  • first_mask / second_image / second_mask (all optional) - a second reference to place alongside the first, plus masks for either side if you need them.
  • patch_mode (default auto) - where the working patch goes: auto, patch_right, or patch_bottom.
  • output_length (default 1536) - the size of the combined canvas.
  • patch_color (default white) - the fill color for the patch region when there's nothing to place there yet.

Outputs: image (the stitched canvas, feed this straight into your edit model), mask (the patch region, for models/nodes that want an explicit mask), and icmask_data - pass this along untouched to IC Mask Crop Back later in the graph.

How to install it

Search ComfyUI Layer Style in ComfyUI Manager, or:

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

Restart; it's under 😺dzNodes → LayerUtility.

Common issues

The most common trap is running the edit model on the stitched canvas but forgetting to carry icmask_data through to IC Mask Crop Back at the end - without it, you're stuck with the full combined image (reference plus patch) instead of just the clean edited result, since IC Mask by itself doesn't return the crop, only the data needed to do it later. If your patch lands in an awkward spot relative to your subject, try forcing patch_mode to patch_right or patch_bottom explicitly rather than trusting auto - auto's aspect-ratio heuristic doesn't always match what you had in mind. And if the edit model's output looks like it's "bleeding" reference content into the patch area in ways you didn't want, check patch_color - a fill color too close to your subject's actual palette can confuse models that are sensitive to color continuity across the seam.

Category😺dzNodes/LayerUtility

Inputs (7)

NameTypeDefaultDescription
first_imageIMAGE—
patch_modeCOMBOauto3 options: auto, patch_right, patch_bottom
output_lengthINT1536—
patch_colorCOMBO#FFFFFF4 options: #FF0000, #00FF00, #0000FF, #FFFFFF
first_maskoptMASK—
second_imageoptIMAGE—
second_maskoptMASK—

Outputs (3)

NameTypeDescription
imageIMAGE—
maskMASK—
icmask_dataICMASK_DATA—