Skip to content

Inpainting

Acly edited this page Feb 8, 2024 · 2 revisions

Selection Fill

Using selections to modify parts of the image is a key feature of the plugin. It allows you to apply AI generation exactly where you want. You can work on an image iteratively, mixing traditional painting tools and generated content as needed.

Generation mode

Creating a selection will transform the "Generate" button to reflect the operation. By default this is either Fill, or Expand, depending on the selection context. But there is also a drop-down which allows you to choose a more specific action which may yield better results.

release-1 14 0-inpaint-menu

Fill

This is a general purpose mode that fits most scenarios where you want to replace a part of the image with something similar. It may generate new things, but is biased towards using colors found in the vicinity of the selection. It tries to strike a balance between flexibility to make changes and blending in with the rest of the image.

Original Mask Result
original fill-mask fill-result

Expand

This mode is geared to expanding the image at the left, right, top or bottom. You can expand into more than one direction at once, but it requires more resources (memory and time). The easiest way to expand the canvas is to use Krita's crop tool (hotkey: C) and click anywhere into the image. You can now resize the canvas at the sides. Select the blank areas and click Expand to generate content.

Original Mask Result
original release-1 14 0-inpaint-expand-mask release-1 14 0-inpaint-expand-image

Expand completely ignores existing content in the selection area, it can be blank or anything else. It attempts to blend in with the colors used at the border of the selection, and prefers to generate more of the same, rather than being overly creative.

Add Content

Use this mode to add new things to the image. This mode requires you to use text or control layers to describe the new content you want to see. It generates with the flexibility to drastically deviate from the colors seen in the rest of the image, and may not blend as well as other modes.

Original Mask Result
original release-1 14 0-inpaint-add-mask release-1 14 0-inpaint-add-image

Remove Content

This mode is geared towards object removal. It ignores existing content in the selection area and fills it with a continuation of the surroundings instead.

Original Mask Result
original release-1 14 0-inpaint-remove-mask release-1 14 0-inpaint-remove-image

Replace Background

An experimental mode where you can select a forground object. The selected content is kept, and everything else in the image is replaced. This mode requires you to use text to describe the new background.

Original Mask Result
original release-1 14 0-inpaint-replace-mask release-1 14 0-inpaint-replace-image

Note: inpaint models aren't trained for this particular use case, complex interactions between foreground/background require some luck.

Custom Generation

If none of the modes quite fit, there is also the option to configure generation parameters manually.

custom-inpaint-menu

  • Seamless: When enabled, an inpaint model is used to make generated content blend in with the existing image as best as possible. In some cases you may want to explicitly disable this, for example when generating a single panel within a comic page. When disabled, generation will behave as if the seleted area was its own image within the canvas, independent of the rest.
  • Focus: When disabled, the text you enter describes the entire context area (see below). Enable this to focus your prompt exclusively on the selected area instead.
  • Fill: This defines how the selected area is filled before generation, and can influence what kind of results are possible.
    • None means the image content is not touched at all, which allows you to paint it over as you see fit.
    • Neutral allows to generate anything without bias.
    • Blur will blur existing and surrounding content together.
    • Border ignores existing content and takes colors only from the surrounding.
    • Inpaint fills the selected area using a small, specialized AI model.

Visualization of the fill modes: (note that these are not final results, they only show pre-processing)

Input Neutral Blur Border Inpaint
preprocess-input preprocess-neutral preprocess-blur-65 preprocess-navier-stokes preprocess-mat

Context area

When generating content for the selected area, part of the surrounding image is processed as "context". This is required to get good results that blend in. A larger context area improves coherency with the rest of the image, at the cost of slower speed and increased memory requirements. You always pay for the entire context even if it isn't touched, not just the masked area.

By default the context is selected automatically. It can be influenced by changing Settings > Diffusion > Selection Padding. Note that there are also internal constraints and tweaks to select a suitable region. Custom Generation offers some additional options:

  • Selection Bounds: use only the selection area. This makes sense when Seamless is disabled, but won't allow it to work properly if it's on.
  • Entire Image: use the entire image as context. Best coherency, but slow and image quality suffers at high resolutions.
  • Custom Layer: use the area indicated by a Selection Mask or Transparency Mask layer. Full control!

Working with Selection Masks

This is an example workflow that uses selection masks to define a custom context.

First, make sure you have "Global Selection Mask" enabled in Krita.

custom-inpaint-global-selection

Use rectangular selection tool to mark your context area.

custom-inpaint-context-selection

Save your selection mask (the dashed circle icon marked below), and optionally give it a nice name. Then create the selection which you want to fill (like some badly generated hands).

custom-inpaint-context-layer

In custom generation menu, select your mask as context. You can reuse it any time you need.

custom-inpaint-context-menu