diffusers/docs/source/using-diffusers/img2img.mdx

47 lines
1.8 KiB
Plaintext
Raw Normal View History

<!--Copyright 2022 The HuggingFace Team. All rights reserved.
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.
-->
# Text-Guided Image-to-Image Generation
2022-09-07 03:31:13 -06:00
The [`StableDiffusionImg2ImgPipeline`] lets you pass a text prompt and an initial image to condition the generation of new images.
2022-09-07 03:31:13 -06:00
```python
from torch import autocast
import requests
from PIL import Image
from io import BytesIO
2022-09-07 03:31:13 -06:00
from diffusers import StableDiffusionImg2ImgPipeline
2022-09-07 03:31:13 -06:00
# load the pipeline
device = "cuda"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16
).to(device)
2022-09-07 03:31:13 -06:00
# let's download an initial image
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
2022-09-07 03:31:13 -06:00
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))
2022-09-07 03:31:13 -06:00
prompt = "A fantasy landscape, trending on artstation"
2022-09-07 03:31:13 -06:00
with autocast("cuda"):
images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5).images
2022-09-07 03:31:13 -06:00
images[0].save("fantasy_landscape.png")
```
2022-09-08 13:29:24 -06:00
You can also run this example on colab [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/image_2_image_using_diffusers.ipynb)
2022-09-07 03:31:13 -06:00