Reference Edit Mask Composite
Keep everything except the bit you changed
- generated
- source
- IMAGE
Editing a photo with a diffusion model has an annoying habit: you ask for a new jacket and the model also "helpfully" redraws the face, the wall and the lighting by 2%. Reference Edit Mask Composite is the node that stops that. It takes the generated image and the original source plus a mask, and returns an image where every pixel outside the mask is the original source, untouched.
It doesn't segment anything and it doesn't generate anything. It's the last step, and it's deliberately boring.
What it is in the wider picture
Most of local editing is getting the region right - masking, inpainting, regional prompting. But a correct mask doesn't guarantee the rest of the frame survives, because the model re-renders the whole image anyway. Compositing the result back over the source is the deterministic answer; a pixel copy doesn't drift the way a re-roll does.
Here you get the pixel copy. If the mask is right, this step is invisible and that's success.
How it works
Everything meaningful happens in about twenty lines of arithmetic, and the guards matter as much as the blend.
- Source identity is checked.
source_refis a Prompt Studio JSON image reference -filename,subfolder, and atypeofinput,output,temporpromptstudio. The node resolves it, hashes the file and compares against source_digest, the SHA-256 recorded when the mask was created. Swap or overwrite the source since, and you getThe source file changed after mask creation. Create a new mask. - Geometry has to line up. The mask PNG is loaded as grayscale and must match the source's height and width exactly, or
Edit mask dimensions no longer match the source. The source must be RGB (3 channels); the generated image can be RGB or RGBA. Batches must be one source against one-or-many generated frames. - The blend. The generated pixels are bilinearly resized to the source's resolution - so output dimensions are the source's, not the sampler's. Then
result = source + (generated - source) * mask, with RGBA generated images multiplying the mask by their own alpha channel. Mask white means "take the new pixels"; mask black means "keep the source."
Because the output is composited at source resolution, an edit run at a different size still lands back on your original dimensions, with no resampling of the preserved area.
Inputs and outputs
All five inputs are required, and in practice you don't type any of them:
- generated (IMAGE) - the decoded output of your edit.
- source (IMAGE) - the original image being edited.
- mask_ref (STRING) - JSON reference to the mask PNG.
- source_ref (STRING) - JSON reference to the source image.
- source_digest (STRING) - SHA-256 of the source file at mask-creation time.
Output: one IMAGE. When Prompt Studio creates the mask for you it inserts this node between your VAE Decode and the image-output node and re-points the saver's images input at it. If you wire it by hand, that's the pattern: decode → composite → save. WebP saves are switched to lossless, presumably because a lossy pass over a region-preserving composite would defeat the point.
Install
The node ships with the pack, so nothing to fetch for the node itself:
cd ComfyUI/custom_nodes
git clone https://github.com/tiko13/ComfyUI_PromptStudio
# restart ComfyUI
Or ComfyUI Manager → search ComfyUI_PromptStudio → restart. The pack adds no runtime pip requirements. What you do need are the models behind the mask:
- SAM ViT-B (
sam_vit_b_01ec64.pth) in ComfyUI'smodels/samsdirectory, for creating the mask. - Optional: Grounding DINO Tiny plus a Transformers runtime, if you want the "find object by name" path to build the selection.
Neither is a dependency of this node - a mask you supply yourself works fine.
Where people get burned
The errors here are all fingerprint mismatches, which is the point of the design:
Create a new maskafter editing or replacing the source image. The digest is doing its job; don't fight it, redo the mask.- Dimension mismatch means the mask came from a different crop or resize of the source.
- Studio refuses more than one active edit mask per generation, and refuses to add a second composite if your workflow already has one:
Workflow already has a reference mask composite. - Studio only offers this on a single direct VAE-Decode-to-image-output graph. A workflow that decodes twice, or saves through a more complicated path, won't get the option - and video workflows keep their own temporal handling instead.
And the honest limitation, straight from the author's own docs: this is compositing, not magic. A tight mask clips clothing or a moved limb and leaves a visible seam. The node guarantees the pixels outside the mask are yours; it can't make the join look natural. Give the mask a little margin.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| generated | IMAGE | — | |
| source | IMAGE | — | |
| mask_ref | STRING | — | |
| source_ref | STRING | — | |
| source_digest | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |