File size: 1,879 Bytes
8d9a1a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2893544
 
 
 
 
8d9a1a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import imageio
import numpy as np
from PIL import Image
from diffusers import AutoPipelineForInpainting
import torch

device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device for I2I: {device}")

# Load the inpainting pipeline

def resize_image(image, height, width):
    """Resize image tensor to the desired height and width."""
    return torch.nn.functional.interpolate(image, size=(height, width), mode='nearest')


def dummy(img):
    """Save the composite image and generate a mask from the alpha channel."""
    imageio.imwrite("output_image.png", img["composite"])

    # Extract alpha channel from the first layer to create the mask
    alpha_channel = img["layers"][0][:, :, 3]
    mask = np.where(alpha_channel == 0, 0, 255).astype(np.uint8)

    return img["background"], mask


def I2I(prompt, image, width=1024, height=1024, guidance_scale=8.0, num_inference_steps=20, strength=0.99):
    
    pipe = AutoPipelineForInpainting.from_pretrained(
        "diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
        torch_dtype=torch.float16, variant="fp16").to(device)

    img_url, mask = dummy(image)

    # Resize image and mask to the target dimensions (height x width)
    img_url = Image.fromarray(img_url, mode="RGB").resize((width, height))
    mask_url = Image.fromarray(mask,mode="L").resize((width, height))

    # Make sure both image and mask are converted into correct tensors
    generator = torch.Generator(device=device).manual_seed(0)

    # Generate the inpainted image
    output = pipe(
        prompt=prompt,
        image=img_url,
        mask_image=mask_url,
        guidance_scale=guidance_scale,
        num_inference_steps=num_inference_steps,  # steps between 15 and 30 work well for us
        strength=strength,  # make sure to use `strength` below 1.0
        generator=generator,
    )

    return output.images[0]