ComfyUI inpainting: edit one area and preserve the rest
ComfyUI inpainting lets you replace an object, repair a detail or remove something from a still image using a painted mask. The difficult part is keeping everything outside that selection genuinely unchanged. A mask can restrict the diffusion process while the decoded image still acquires subtle colour or texture shifts elsewhere.
This tutorial uses a small, downloadable workflow that composites the generated region back over the original image. It also separates two tests that are often confused: exact pixel preservation outside the mask and whether the edited boundary looks convincing. You can create a source with the DIY AI image generator if needed, but the local masking and compositing steps below are performed in ComfyUI.
The workflow: sample inside the mask, then composite onto the original
Recommended starting configuration: an inpainting-compatible Stable Diffusion checkpoint, a 1024 x 640 PNG, two masks and ComfyUI’s built-in nodes. The example JSON uses 512-inpainting-ema.safetensors and a fixed seed so the same setup can be repeated. It does not require third-party custom nodes.
| Stage | Node or setting | Purpose |
|---|---|---|
| Load | Load Image + two Load Image Mask nodes | Keep the original RGB image, the hard selection and the softer blend selection separate. |
| Prepare | VAE Encode (for Inpainting), grow mask by 8 | Give the sampler some context around the selected region. |
| Generate | KSampler: 24 steps, CFG 7, Euler, normal, denoise 1.0, seed 424242 | Start with a strong replacement pass rather than a near-identical reconstruction. |
| Preserve | VAE Decode to Image Composite Masked | Use the untouched source as destination and the new pixels as source. |
| Inspect | Two Save Image nodes | Export both the raw decoded generation and the final preserved composite. |
The important connection is ImageCompositeMasked.destination = original image, source = VAE Decode and mask = feathered blend mask. Without this final step, the sampler’s masked operation should not be treated as a guarantee that every unselected output pixel matches the input.
Download the ComfyUI inpainting workflow and preservation test
The versioned pack contains comfyui-preserve-outside-mask-v1.0.0.json, scene-source.png, mask-hard.png, mask-feather-inwards.png, a known clean-plate reference, a synthetic composite, a deliberately flawed composite, and compare_pixels.py. It also contains a JSON/CSV metrics report and instructions.
Test integrity: the included example composites are deterministic raster demonstrations, not images generated by a ComfyUI checkpoint. They verify the pixel-comparison method and expose a colour-seam failure. The workflow’s topology and node types were checked against ComfyUI’s published examples, but an actual checkpoint generation has not been executed for this pack. Run the workflow locally and evaluate its output separately before presenting it as a model benchmark.
How to run the JSON in ComfyUI without rebuilding the graph
- Place the three input files in ComfyUI’s input folder:
scene-source.png,mask-hard.pngandmask-feather-inwards.png. Keep their dimensions unchanged. - Import the workflow JSON by dropping it onto the ComfyUI canvas or loading it through the workflow menu. Re-select the image and mask filenames if your instance does not resolve them automatically.
- Install or select a compatible inpainting checkpoint in the Checkpoint Loader. The example uses ComfyUI’s documented Stable Diffusion inpainting checkpoint. If you prefer a different model family, use its own supported inpainting template instead of assuming the same VAE and text-conditioning nodes apply.
- Check that the hard white mask feeds VAE Encode (for Inpainting), while the separate inward-feathered mask feeds Image Composite Masked. White selects pixels to replace; black preserves the source. The blend file uses grey values only within the hard mask.
- Queue the prompt. Save the raw output and final composite as lossless PNG files. Compare the latter against the original using
compare_pixels.py, then inspect the repaired edge at 200% or 400% zoom.
The official ComfyUI inpainting tutorial explains the native mask editor and inpainting encoder if you prefer to paint the mask yourself. Our separate mask files are intentional: they make the exact editable area reproducible and prevent a feathering change from silently redefining the preservation test.
The preservation result: zero changed pixels can still conceal a bad edge
For the controlled raster demonstration, the source is 1024 x 640 pixels. The hard mask covers 70,287 pixels, leaving 585,073 outside the selection. Comparing the source PNG against the composited PNG gives 0 changed RGB pixels outside the hard mask, including the 12-pixel inspection ring immediately beyond its edge. This measures our deterministic compositing example, not the output quality or consistency of ComfyUI.
That strict result is useful but insufficient. A pixel comparison cannot tell you whether the model reconstructed a plausible wall, whether an object has left a shadow or whether the colour of a replacement matches the surroundings. It only answers whether the pixels you designated as protected have changed.
We therefore inspected an additional 14-pixel band inside the mask against a known clean plate. In the synthetic demonstration, the inward-feathered composite has a mean absolute RGB channel error of 1.951 in that band. The deliberately colour-shifted, hard-edged example rises to 10.333, although both examples leave every pixel outside the hard mask unchanged. Both figures come from constructed raster fills and should not be interpreted as generative model scores.
The example also leaves a small amount of original cast-shadow evidence just beyond the protected boundary. A zero-difference result is therefore not the same as a complete object removal. If the unwanted object’s shadow extends past your mask, preserving those pixels exactly preserves the defect as well.
Why the raw decoded image can change even where the mask is black
In a conventional masked latent workflow, the mask influences what the sampler regenerates. The input image still travels through VAE encoding and decoding, and that reconstruction can differ slightly from the original even where noise was restricted. A preview may look identical at normal zoom while a direct RGB comparison reveals small changes.
The remedy is architectural, not prompt-based. Keep a direct path from the original Load Image node to the compositing destination. Let the sampler produce a candidate edit, then replace only the selected pixels in the final image. Do not send the finished composite through another image-to-image pass, global colour adjustment, resizer or lossy JPEG export before running a strict comparison. Any of those operations can legitimately alter the protected area again.
Feather inward if exact outside-mask preservation is the requirement
Feathering makes the transition less abrupt by blending original and generated pixels over a narrow strip. But a normal outward blur extends some non-zero mask values beyond the painted boundary. Those outside pixels will then change, even when the compositor is working correctly. This is why vague claims of “nothing outside the mask changed” often collapse when you inspect the actual mask values.
Our two-mask approach uses a hard mask to define the protected area and a second mask that fades inward over 14 pixels. The blend mask is exactly black outside the hard selection. That protects the outside pixels while giving the newly generated region a transitional edge. It is particularly useful for walls, skies and broad surfaces where an unexpected halo is easy to spot.
It is not always the prettiest choice. If the object, its shadow or a reflection reaches the selection edge, inward feathering may preserve unwanted remnants. In that case, expand the hard mask first, regenerate with enough surrounding context and then construct a new inward feather from the expanded boundary. Do not secretly expand the blend mask after recording the area you claim to preserve.
What the failed example tells you about denoise, colour and models
The pack’s failed example intentionally adds a warm colour offset to the replacement patch and applies it with a hard boundary. It produces an obvious rectangle. Reducing the denoise value would not repair that constructed colour shift, because denoise controls generation, not how a mismatched patch is blended into the source.
| Visible failure | Check first | Next action |
|---|---|---|
| Unmasked background looks subtly different | Is the exported file coming directly from VAE Decode? | Reattach the original image as composite destination; save a lossless final PNG. |
| Object or lettering remains inside the mask | Is the mask correctly oriented and is denoise too low? | Confirm white means edit; try a higher denoise value or a dedicated inpainting checkpoint. |
| Replacement is correct but a bright border remains | Does the feathered edge contain a colour or texture mismatch? | Match colour/exposure, revise the blend strip and add useful context before changing models. |
| Shadow or reflection is still visible | Does secondary scene evidence fall outside the hard mask? | Expand the actual edit region; an exact-pixel guarantee cannot also erase protected shadows. |
| Repeated attempts cannot reconstruct the missing texture | Is the masked object hiding unique geometry or a patterned surface? | Use a clean plate, manual texture repair or a better-suited inpainting model rather than endlessly changing seeds. |
Start with a fixed seed and one variable per run. Increase denoise when the intended replacement is not happening; change the model when the task consistently exceeds its ability to complete the selected region. For a visible seam with otherwise correct content, fix the composite and colour relationship first. A different checkpoint can make the patch more convincing, but it cannot guarantee exact preservation unless the workflow still uses the original-pixel composite.
See more of our reporting in Google Top Stories, AI Overviews and AI Mode.
What to check before accepting the exported edit
- Source, raw generation, output and masks have the same pixel dimensions and are aligned.
- The hard mask covers the target object, its shadow and any unwanted reflections that must disappear.
- The blend mask contains no grey pixels outside the region you promise to preserve.
- The ComfyUI graph saves both the uncomposited candidate and the original-backed composite.
- The final comparison runs on lossless PNG files, not JPEG previews or resized website images.
- The outer pixel-difference test passes, and the inward edge band passes visual inspection at high zoom.
If you need general model-selection advice, compare editing approaches in our AI image editing models guide. For installation and hardware choices rather than masked image repair, see our local AI image generator guide. Moving footage is a different problem: our video inpainting testing guide covers temporal drift and tracking.
ComfyUI inpainting FAQs
Can ComfyUI preserve every pixel outside a mask?
Yes, as an export-level goal: composite the candidate image over the untouched original using a blend mask that is exactly zero outside the protected area, then save losslessly at the same resolution. Verify with a pixel comparison rather than relying on the mask’s appearance in the editor.
Does feathering always modify pixels outside the mask?
No. An outward feather can, because it gives previously black pixels a small blend value. An inward feather is confined to the selected region. Which is preferable depends on whether the priority is strict preservation or hiding a difficult transition.
Can I substitute FLUX or Qwen into this workflow?
Not reliably by changing the checkpoint filename alone. Different model families can require different encoders, conditioning, latent handling and inpainting workflows. Keep the preservation principle, but start from the model’s own compatible graph and attach the original-backed compositor at the end.
The practical acceptance rule
Use two separate pass/fail checks. First, prove that the final PNG has not changed any RGB pixels outside the declared hard mask. Second, inspect the repaired area and its 14-pixel internal border against the source and, where possible, a clean plate. Only accept the edit if both tests pass. A preserved background with a glaring rectangle is still a failed inpaint, and a beautiful replacement that subtly recolours the rest of the photograph is not a preservation pass.

