Inpainting: redraw part of a picture
Lesson "inpaint", in the group Pictures: techniques of the lesson list. Steps: 10.
What you will be able to do
You paint a mask over part of a picture, and the model draws only that part again, by a new prompt.
What you need
- It goes on from A picture from a picture. The card says so; it is a hint, not a lock.
- Nodes: Load Checkpoint, CLIP Text Encode (Prompt), KSampler, VAE Decode, Save Image / Preview Image, Load Image, InpaintModelConditioning.
- The app downloads and installs nothing. The step texts below say which files and folders, and which ComfyUI template names them.
The steps
The words of each step, as the lesson shows them. Node names are in bold.
What inpainting is (reading)
Inpainting draws again the part of a picture under a mask and leaves the rest as it was. The mask is painted by hand in ComfyUI or made by a node that finds an object; that is the lesson "A mask by words".
This lesson goes on from "A picture from a picture" and puts the inpainting node where the plain encoding was.
The graph of a picture from a picture is needed
The canvas must have Load Checkpoint, the texts CLIP Text Encode (Prompt) and CLIP Text Encode (Prompt), KSampler, VAE Decode, Save Image and Load Image, as "A picture from a picture" leaves them.
If they are not there, do that lesson first.
Paint the mask in ComfyUI (reading)
The mask is painted in ComfyUI, not in this app. Open the graph there, right-click the Load Image node and choose "Open in MaskEditor". Paint over what must change, a little wider than the object, and press "Save".
ComfyUI keeps the picture with its mask in the
inputfolder, and theMASKoutput of the node carries the mask. A picture with a transparent area works the same: the transparent part is the mask.This app does not show the mask. The graph is the same either way.
Which model draws a part best (reading)
Any model can draw under a mask, but the border often shows. Models trained for inpainting see the picture around the mask and blend better: for SD 1.5 the checkpoints with "inpainting" in the name, for Flux the Flux.1 Fill model (it has separate loaders: open the official "Flux.1 Fill Dev" template in ComfyUI to see its graph).
An inpainting checkpoint goes into the same Load Checkpoint node as an ordinary one.
Add the inpainting node
Add an InpaintModelConditioning node and connect to it:
- to
positiveandnegative, the two texts; - to
vae, theVAEoutput of Load Checkpoint; - to
pixels, the picture, through the scaling node when the picture goes through it; - to
mask, theMASKoutput of Load Image.
The node encodes the picture, marks the masked part in the latent and tells an inpainting model what lies around it. Leave
noise_maskon: then only the masked part is drawn again.- to
Feed the sampler from it
Connect the three outputs of InpaintModelConditioning to KSampler:
positivetopositive,negativetonegative,latenttolatent_image.The encoding node of the previous lesson is not needed any more: its link to the sampler is replaced.
Set the strength
Set
denoiseof KSampler to 1 for an inpainting model. For an ordinary model take 0.6 to 0.8: the colours of the old content then show through and help it fit.If the masked part comes out the same as before, raise
denoise. If it comes out foreign to the picture, lower it or take an inpainting model.Describe what should be there (reading)
Describe the whole picture with the new content, not an order to replace something. For a hat on a man: "a man in a red wool hat on a city street". The model sees the rest of the picture and fits the new part to it.
The prompt can be short: the picture around says the rest.
When the border shows (reading)
What helps:
- paint the mask wider than the object; a Grow Mask node widens it by a few pixels;
- a Differential Diffusion node between the model and the sampler softens the edge of the mask, as ComfyUI's Flux Fill template does;
- for a small part of a large picture, the ComfyUI-Inpaint-CropAndStitch pack cuts out the area around the mask, draws it at the size the model likes and puts it back.
A node pack runs other people's code on your computer. Install it yourself through ComfyUI's manager, from its official page, and only if you trust its authors. The app installs nothing.
The graph is ready (reading)
Save the graph with "File ▸ Save workflow for ComfyUI…", open the file in ComfyUI and run it there. This app runs nothing in ComfyUI itself.
What to try: the same mask with three different seed values; a mask painted tight and a mask painted wide.
What the lesson checks
A reading step is done when you press Got it, next. A doing step ticks itself off when the canvas holds:
- Step 2, The graph of a picture from a picture is needed:
- a Load Checkpoint node.
- a CLIP Text Encode (Prompt) node.
- a CLIP Text Encode (Prompt) node.
- a KSampler node.
- a VAE Decode node.
- a Save Image or Preview Image node.
- a Load Image node.
CLIPof Load Checkpoint intoclipof CLIP Text Encode (Prompt) (prompt) (a Load LoRA or CLIP Set Last Layer in between is accepted).CLIPof Load Checkpoint intoclipof CLIP Text Encode (Prompt) (negative prompt) (a Load LoRA or CLIP Set Last Layer in between is accepted).MODELof Load Checkpoint intomodelof KSampler (a Load LoRA or LoraLoaderModelOnly or DifferentialDiffusion or ModelSamplingAuraFlow or ModelSamplingFlux or CFGNorm or IPAdapter Unified Loader or IPAdapter or IPAdapterAdvanced or IPAdapterStyleComposition in between is accepted).CONDITIONINGof CLIP Text Encode (Prompt) (prompt) intopositiveof KSampler (a Apply ControlNet or InpaintModelConditioning or Conditioning (Combine) or ConditioningSetArea or ConditioningSetAreaPercentage or ConditioningSetMask or Apply Style Model or FluxGuidance or ConditioningZeroOut or FluxKontextMultiReferenceLatentMethod in between is accepted).CONDITIONINGof CLIP Text Encode (Prompt) (negative prompt) intonegativeof KSampler (a Apply ControlNet or InpaintModelConditioning or Conditioning (Combine) or ConditioningSetArea or ConditioningSetAreaPercentage or ConditioningSetMask or Apply Style Model or FluxGuidance or ConditioningZeroOut or FluxKontextMultiReferenceLatentMethod in between is accepted).LATENTof KSampler intosamplesof VAE Decode.VAEof Load Checkpoint intovaeof VAE Decode.IMAGEof VAE Decode intoimagesof Save Image or Preview Image.
- Step 5, Add the inpainting node:
- a InpaintModelConditioning node.
CONDITIONINGof CLIP Text Encode (Prompt) (prompt) intopositiveof InpaintModelConditioning (a Apply ControlNet or InpaintModelConditioning or Conditioning (Combine) or ConditioningSetArea or ConditioningSetAreaPercentage or ConditioningSetMask or Apply Style Model or FluxGuidance or ConditioningZeroOut or FluxKontextMultiReferenceLatentMethod in between is accepted).CONDITIONINGof CLIP Text Encode (Prompt) (negative prompt) intonegativeof InpaintModelConditioning (a Apply ControlNet or InpaintModelConditioning or Conditioning (Combine) or ConditioningSetArea or ConditioningSetAreaPercentage or ConditioningSetMask or Apply Style Model or FluxGuidance or ConditioningZeroOut or FluxKontextMultiReferenceLatentMethod in between is accepted).VAEof Load Checkpoint intovaeof InpaintModelConditioning.IMAGEof Load Image intopixelsof InpaintModelConditioning (a Upscale Image or Upscale Image By or Scale Image to Total Pixels or FluxKontextImageScale or Resize Image or ImageResizeKJv2 or Pad Image for Outpainting in between is accepted).MASKof Load Image intomaskof InpaintModelConditioning (a GrowMask or FeatherMask or Invert Mask or ThresholdMask in between is accepted).
- Step 6, Feed the sampler from it:
positiveof InpaintModelConditioning intopositiveof KSampler (a Apply ControlNet or InpaintModelConditioning or Conditioning (Combine) or ConditioningSetArea or ConditioningSetAreaPercentage or ConditioningSetMask or Apply Style Model or FluxGuidance or ConditioningZeroOut or FluxKontextMultiReferenceLatentMethod in between is accepted).negativeof InpaintModelConditioning intonegativeof KSampler (a Apply ControlNet or InpaintModelConditioning or Conditioning (Combine) or ConditioningSetArea or ConditioningSetAreaPercentage or ConditioningSetMask or Apply Style Model or FluxGuidance or ConditioningZeroOut or FluxKontextMultiReferenceLatentMethod in between is accepted).latentof InpaintModelConditioning intolatent_imageof KSampler.
- Step 7, Set the strength:
denoiseof KSampler is from 0.5 to 1; Do this for me sets 1.
What next
These lessons go on from this one: Outpainting: a picture beyond its edges, A mask by words.