Skip to content

Commit 3e2961f

Browse files
yiyixuxuyiyixuxu
andauthored
[doc] update inpaint doc to use apply_overlay (huggingface#6364)
add doc Co-authored-by: yiyixuxu <yixu310@gmail,com>
1 parent 79c380b commit 3e2961f

File tree

1 file changed

+2
-13
lines changed

1 file changed

+2
-13
lines changed

docs/source/en/using-diffusers/inpaint.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ make_image_grid([init_image, image], rows=1, cols=2)
318318

319319
The trade-off of using a non-inpaint specific checkpoint is the overall image quality may be lower, but it generally tends to preserve the mask area (that is why you can see the mask outline). The inpaint specific checkpoints are intentionally trained to generate higher quality inpainted images, and that includes creating a more natural transition between the masked and unmasked areas. As a result, these checkpoints are more likely to change your unmasked area.
320320

321-
If preserving the unmasked area is important for your task, you can use the code below to force the unmasked area of an image to remain the same at the expense of some more unnatural transitions between the masked and unmasked areas.
321+
If preserving the unmasked area is important for your task, you can use the `apply_overlay` method of [`VaeImageProcessor`] to force the unmasked area of an image to remain the same at the expense of some more unnatural transitions between the masked and unmasked areas.
322322

323323
```py
324324
import PIL
@@ -345,18 +345,7 @@ prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
345345
repainted_image = pipeline(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
346346
repainted_image.save("repainted_image.png")
347347

348-
# Convert mask to grayscale NumPy array
349-
mask_image_arr = np.array(mask_image.convert("L"))
350-
# Add a channel dimension to the end of the grayscale mask
351-
mask_image_arr = mask_image_arr[:, :, None]
352-
# Binarize the mask: 1s correspond to the pixels which are repainted
353-
mask_image_arr = mask_image_arr.astype(np.float32) / 255.0
354-
mask_image_arr[mask_image_arr < 0.5] = 0
355-
mask_image_arr[mask_image_arr >= 0.5] = 1
356-
357-
# Take the masked pixels from the repainted image and the unmasked pixels from the initial image
358-
unmasked_unchanged_image_arr = (1 - mask_image_arr) * init_image + mask_image_arr * repainted_image
359-
unmasked_unchanged_image = PIL.Image.fromarray(unmasked_unchanged_image_arr.round().astype("uint8"))
348+
unmasked_unchanged_image = pipeline.image_processor.apply_overlay(mask_image, init_image, repainted_image)
360349
unmasked_unchanged_image.save("force_unmasked_unchanged.png")
361350
make_image_grid([init_image, mask_image, repainted_image, unmasked_unchanged_image], rows=2, cols=2)
362351
```

0 commit comments

Comments
 (0)