HowDoesAIDoThat?

Guide / Image control

How to inpaint an image with a mask

Change a red teapot to turquoise while keeping the surrounding kitchen. Follow the mask, local recipe and before-and-after example, then try your own edit.

By HowDoesAIDoThatSources checked 2026-10-11
Red ceramic teapot in a kitchen.
Before
Turquoise teapot in the same kitchen.
After
Steps at a glance
  1. 01 / processPrepare

    Permitted picture and matching inpainting model

  2. 02 / processSelect

    Save a mask over the intended edit

  3. 03 / processDescribe

    Prompt and recorded sampler settings

  4. 04 / processInspect

    Check seams, save image and export recipe

Change one part of an image

Like the picture, but want to change the teapot? Inpainting lets you select that part and ask the model to redraw it. The mask shows where to work; the prompt describes the replacement.

Our worked example replaces a red teapot with a turquoise one while keeping the kitchen outside the selection. Start with a picture you own or have permission to edit. Below, see the actual image, selection and result before choosing a local or hosted route.

See the edit: red teapot to turquoise

The teapot below is a fictional scene we made for this exercise. We loaded that actual image into ComfyUI, repainted a selected region with Z-Image Turbo, then composited the accepted edit over the original. Follow the three pictures: the original, the white editing region, and the result.

Red ceramic teapot on an oak worktop in a softly lit kitchen.
Before
Soft-edged white rectangular selection on a black background, surrounding the original teapot.
Mask: white changes, black stays
Turquoise ceramic teapot in the same kitchen scene.
After

The model redraws the selected area: the new teapot has a different body, spout and lid as well as a new colour. Use this method for a replacement or creative edit. It would need further checks if the exact shape of a real product had to stay unchanged.

Recreate our local Z-Image edit

  1. Prepare the three matching files: z_image_turbo_int8_convrot.safetensors in models/diffusion_models, qwen_3_4b_fp8_mixed.safetensors in models/text_encoders, and ae.safetensors in models/vae. Our files match the published Comfy-Org package. Check your hardware before downloading; no weights are included here.
  2. Download our original teapot PNG and mask PNG. Use two Load Image nodes. Send the mask image through Image to Mask, selecting red; its white area is the region to repaint.
  3. Load the diffusion model with Load Diffusion Model, the text encoder with Load CLIP (lumina2), and the VAE with Load VAE. Send the model through Model Sampling AuraFlow, shift 3, sampling flow.
  4. Encode the prompt below with CLIP Text Encode. Connect it as KSampler's positive input; feed the same conditioning through Conditioning Zero Out for the negative input. Connect the original image, VAE and red-channel mask to VAE Encode (for Inpainting), with grow_mask_by set to 6. Its latent output goes to KSampler.
  5. Set KSampler to seed 26101218, fixed seed control, 8 steps, CFG 1, res_multistep, simple, denoise 1 and batch one. Decode its samples with VAE Decode and the same VAE.
  6. Add Image Composite Masked: original image as destination, decoded image as source, the red-channel mask as mask, x=0, y=0, resize_source=false. Connect this finished image to Save Image. This finishing step puts the edit into the original scene instead of accepting every pixel from the latent decode.
  7. Run, inspect the spout, lid and handle, then save the PNG and export your canvas workflow. Reopen the saved file outside ComfyUI. Keep the original and mask as well as the result.
A turquoise teal ceramic teapot on an oak kitchen counter, rounded ceramic body, matching turquoise lid, curved handle to the right, spout pointing left. Photorealistic soft natural window lighting from the left, sage green wall, linen cloth on the right. No words or watermark.

The API graph contains the nodes and settings used for our result. Follow the wiring above in the canvas, or run the API file with the supplied helper below.

To run the supplied graph directly, download and extract the reader pack below. Copy its inputs/HowDoesAIDoThat_visuals_20261012 folder into your own ComfyUI/input folder. With ComfyUI running locally at http://127.0.0.1:8188, open a terminal in the extracted pack and run:

python run_workflow.py teapot-after-refined.api.json

The helper submits this graph to your local ComfyUI, waits for its history and prints the output filename. Open that file beneath ComfyUI/output. Review the supplied script and graph before running them; they include no remote generation node or model downloader. The helper stops if another job is already queued. An unresolved timeout does not cancel a job: inspect ComfyUI before submitting again.

Why the first mask needed changing

Our first selection cut across the handle. The generated join was visibly awkward, even though the colour changed. We widened the selection to include the whole handle, reran the edit and used the accepted result above.

Earlier turquoise teapot edit with an awkward handle boundary.
First attempt: the handle needed more room

A useful mask surrounds the whole object, with room for its edges. Our rectangular selection includes some background and worktop, so those areas can change too. Its 3-pixel soft edge blends the replacement into the scene. For a closer edit, paint a more precise outline and inspect the handle, lid and spout before exporting.

Alternative: the official dedicated inpainting recipe

Prefer to start from ComfyUI's own tutorial? The dedicated-checkpoint recipe below is a documented alternative to our Z-Image example.

Local editing or hosted compute?

Free local: ComfyUI includes the mask editor and the nodes used here. You supply the compatible hardware and download the model. Read local AI requirements before a large download.

Hosted alternative: Comfy Cloud has a free tier and paid subscriptions; check your account's allowance and model availability. The pricing page restricts custom imports to Creator, Pro or Team plans. Confirm the exact checkpoint is supported before paying.

Prepare the right model and workflow

  1. Install and open ComfyUI using its official installation guidance. Note your software and frontend versions beside the workflow.
  2. Open the official inpainting tutorial. Download its workflow-bearing image using the provided link and load it with Workflows → Open, or drag the original image onto the canvas. Preserve the downloaded original; a recompressed preview may lose its workflow metadata.
  3. The tutorial names 512-inpainting-ema.safetensors. Read the repository's model and licence information, then place that exact file in ComfyUI/models/checkpoints. Windows Portable uses ComfyUI_windows_portable/ComfyUI/models/checkpoints; Desktop users should consult the Storage panel for their configured model folders.
  4. Refresh with R or restart and select the checkpoint in Load Checkpoint. Follow this tutorial's model/graph pair rather than substituting an SDXL, Flux or an arbitrary SD1.5 checkpoint. The linked file is published in a Stable Diffusion 2.1 repackaged repository; the similar-looking names do not make model families interchangeable.

This recipe uses VAE Encode (for Inpainting). Keep the tutorial's model and graph together; other model families use different conditioning.

Load a picture and draw the mask

  1. Upload your original image to Load Image. For the first exercise, use a small copy around 512 × 512 so inspection and memory use are manageable. Keep the original untouched; crop or resize the copy deliberately rather than stretching it.
  2. Right-click Load Image and choose Open in Mask Editor, or use its mask toolbar button. The current editor documentation distinguishes the Mask Pen from the Paint Pen. Choose Mask Pen: painting the RGB layer changes the input rather than merely selecting the edit area.
  3. Paint over the teapot, leaving the surrounding kitchen unselected. Include the lid, spout and handle edges that need replacing. Use the eraser to remove accidental mask marks. Start with one simple region rather than several unrelated fixes.
  4. Click Save inside the editor. Closing or cancelling without saving will not apply your new mask. The visible overlay can be black, white or inverted for display; do not infer mask meaning from overlay colour alone.

If the opposite area changes, inspect the mask and inversion nodes. In the core encoder implementation, mask value 1 selects the editable region and 0 leaves it unselected. Overlay colour is a separate display setting.

Check the graph and set the replacement

Load Image's IMAGE goes to pixels and its MASK to mask on VAE Encode (for Inpainting). Load Checkpoint's VAE goes to that encoder and to VAE Decode. The encoder's LATENT goes to KSampler's latent_image. The checkpoint MODEL and the two prompt-conditioning outputs also feed KSampler. Its result is decoded, then passed to Save Image.

Use the original task prompt in the positive text encoder:

A turquoise ceramic teapot on an oak kitchen worktop, matching lid, curved handle and spout, soft natural window light, sage green wall.

This describes the replacement and surrounding subject. Put text, watermark, duplicate subject in the negative encoder; those preferences are not guaranteed exclusions.

For the first run, retain the upstream workflow's sampler and conditioning settings and record their actual values. Choose seed 424242 and fixed seed control for our comparison exercise. Keep batch size one. grow_mask_by expands the editable boundary; start with the template's value, note it, and change it only if you need a wider transition. Too much expansion can include details you intended to keep.

Run, inspect and export

  1. Click Run or use Ctrl + Enter and wait for the queue to complete. If a node fails, retain the exact message and check the model and connections before another attempt.
  2. Inspect the teapot, but also compare the cloth, worktop and background with the original. Look for visible seams or unexpected changes. The mask constrains the editing process; this latent decode workflow does not promise pixel-identical unmasked areas.
  3. If the teapot barely changes, inspect mask coverage first. Adjusting denoise can affect how much the input is changed, but its effect depends on this model and graph. Keep a record and change one variable per attempt instead of treating a single denoise number as universally correct.
  4. Save the output image from Save Image's menu, open it locally, and export the graph using Workflows → Export. The local output folder is ComfyUI/output. Keep the original, edited PNG, mask, prompt and teapot-inpaint-workflow.json together.

To keep the original outside the selection, composite the accepted edit over it as our worked graph does, then inspect the join. Keep the original and mask until you are happy with the result.

Run the hosted version

In Comfy Cloud, load the official workflow, confirm the checkpoint's availability, upload your permitted image and redraw or import the mask. Check the same wiring, prompt and recorded settings. Run, download the output through its image menu and export the workflow. Uploading the image sends it to a provider, which matters for private material.

Paid subscriptions spend credits when the workflow runs. Check the current terms and allow for another attempt when planning a small editing session.

Fix common editing problems

  • Nothing changes: confirm you saved the mask, the MASK output is connected and the target area is selected.
  • The wrong region changes: check inversion and mask display versus actual mask data.
  • A hard seam appears: review mask coverage and grow_mask_by; test a small adjustment while preserving the previous result.
  • The whole picture changes: compare model family, graph and mask connections. Keep the original for a final composite if exact outside pixels matter.
  • Missing model or nodes: verify the exact official graph and checkpoint; use the missing-node question rather than installing unrelated packs.

Keep the original, mask, edited PNG and workflow together. That gives you a clean starting point for a smaller correction or another colour.

Choose a route

RouteCost basisWhat you needWhat changes
Local ComfyUIFree localNo ComfyUI subscription; own hardware, electricity and model storage.Compatible local hardware and exact model files; check installation requirements.More setup and troubleshooting; local-model nodes need no paid API.
Comfy CloudPaid hostedPaid subscription plus runtime credits; a limited free tier is advertised. Check live terms.Account, available compatible models; Creator, Pro or Team for custom imports.No local GPU required; uploads leave your device and allowances vary.

Provider links are ordinary links. Free local software still needs hardware, storage and setup time; inspect current provider terms before paying.

Editable starting prompt

Change the subject and details to suit your picture, then use the settings in the guide.

A turquoise teal ceramic teapot on an oak kitchen counter, rounded ceramic body, matching turquoise lid, curved handle to the right, spout pointing left. Photorealistic soft natural window lighting from the left, sage green wall, linen cloth on the right. No words or watermark.

Take the steps with you

Reader starter files

Teapot API graph, original PNG, mask and local submission helper, plus the prompt and checklist. Copy the input folder into ComfyUI/input and obtain the model files separately.

Download the starter pack

Sources and method

Primary references checked 2026-10-11. Our local Z-Image example shows the original teapot, mask and finished composite. The dedicated-checkpoint and cloud sections are documented alternatives.