"examples/vscode:/vscode.git/clone" did not exist on "58237364b1780223f48a80256f56408efe7b59a0"
inpaint.md 3.33 KB
Newer Older
Patrick von Platen's avatar
Patrick von Platen committed
1
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
Nathan Lambert's avatar
Nathan Lambert committed
2
3
4
5
6
7
8
9
10
11
12

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

13
# Text-guided image-inpainting
Patrick von Platen's avatar
Patrick von Platen committed
14

15
16
17
18
19
[[open-in-colab]]

The [`StableDiffusionInpaintPipeline`] allows you to edit specific parts of an image by providing a mask and a text prompt. It uses a version of Stable Diffusion, like [`runwayml/stable-diffusion-inpainting`](https://huggingface.co/runwayml/stable-diffusion-inpainting) specifically trained for inpainting tasks.

Get started by loading an instance of the [`StableDiffusionInpaintPipeline`]:
Patrick von Platen's avatar
Patrick von Platen committed
20

21
```python
22
import PIL
23
24
25
import requests
import torch
from io import BytesIO
26
27

from diffusers import StableDiffusionInpaintPipeline
Patrick von Platen's avatar
Patrick von Platen committed
28

29
30
31
pipeline = StableDiffusionInpaintPipeline.from_pretrained(
    "runwayml/stable-diffusion-inpainting",
    torch_dtype=torch.float16,
32
    use_safetensors=True,
33
34
35
36
37
)
pipeline = pipeline.to("cuda")
```

Download an image and a mask of a dog which you'll eventually replace:
Patrick von Platen's avatar
Patrick von Platen committed
38

39
```python
40
41
42
def download_image(url):
    response = requests.get(url)
    return PIL.Image.open(BytesIO(response.content)).convert("RGB")
Patrick von Platen's avatar
Patrick von Platen committed
43
44


45
46
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
Patrick von Platen's avatar
Patrick von Platen committed
47

48
49
init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))
50
```
Patrick von Platen's avatar
Patrick von Platen committed
51

52
Now you can create a prompt to replace the mask with something else:
53

54
```python
55
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
56
image = pipeline(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
57
```
58

59
`image`          | `mask_image` | `prompt` | output |
60
:-------------------------:|:-------------------------:|:-------------------------:|-------------------------:|
61
<img src="https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" alt="drawing" width="250"/> | <img src="https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" alt="drawing" width="250"/> | ***Face of a yellow cat, high resolution, sitting on a park bench*** | <img src="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/in_paint/yellow_cat_sitting_on_a_park_bench.png" alt="drawing" width="250"/> |
62

Patrick von Platen's avatar
Patrick von Platen committed
63

64
<Tip warning={true}>
65
66
67

A previous experimental implementation of inpainting used a different, lower-quality process. To ensure backwards compatibility, loading a pretrained pipeline that doesn't contain the new model will still apply the old inpainting method.

68
</Tip>
69
70
71
72
73
74
75
76
77

Check out the Spaces below to try out image inpainting yourself!

<iframe
	src="https://runwayml-stable-diffusion-inpainting.hf.space"
	frameborder="0"
	width="850"
	height="500"
></iframe>