Diffusers documentation

Text-guided 이미지 인페인팅(inpainting)

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Text-guided 이미지 인페인팅(inpainting)

StableDiffusionInpaintPipeline은 마스크와 텍스트 프롬프트를 제공하여 이미지의 특정 부분을 편집할 수 있도록 합니다. 이 기능은 인페인팅 작업을 위해 특별히 훈련된 runwayml/stable-diffusion-inpainting과 같은 Stable Diffusion 버전을 사용합니다.

먼저 StableDiffusionInpaintPipeline 인스턴스를 불러옵니다:

import PIL
import requests
import torch
from io import BytesIO

from diffusers import StableDiffusionInpaintPipeline

pipeline = StableDiffusionInpaintPipeline.from_pretrained(
    "runwayml/stable-diffusion-inpainting",
    torch_dtype=torch.float16,
)
pipeline = pipeline.to("cuda")

나중에 교체할 강아지 이미지와 마스크를 다운로드하세요:

def download_image(url):
    response = requests.get(url)
    return PIL.Image.open(BytesIO(response.content)).convert("RGB")


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"

init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))

이제 마스크를 다른 것으로 교체하라는 프롬프트를 만들 수 있습니다:

prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
image mask_image prompt output
drawing drawing Face of a yellow cat, high resolution, sitting on a park bench drawing

이전의 실험적인 인페인팅 구현에서는 품질이 낮은 다른 프로세스를 사용했습니다. 이전 버전과의 호환성을 보장하기 위해 새 모델이 포함되지 않은 사전학습된 파이프라인을 불러오면 이전 인페인팅 방법이 계속 적용됩니다.

아래 Space에서 이미지 인페인팅을 직접 해보세요!