fffiloni commited on
Commit
a7d8817
1 Parent(s): 19ec5a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -9,6 +9,8 @@ from huggingface_hub import hf_hub_download
9
  from controlnet_union import ControlNetModel_Union
10
  from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
11
 
 
 
12
  MODELS = {
13
  "RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
14
  }
@@ -55,13 +57,31 @@ prompt = "high quality"
55
 
56
  @spaces.GPU
57
  def fill_image(image, model_selection):
58
- source = image["background"]
59
- mask = image["layers"][0]
60
 
61
- alpha_channel = mask.split()[3]
62
- binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
63
- cnet_image = source.copy()
64
- cnet_image.paste(0, (0, 0), binary_mask)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  for image in pipe(
67
  prompt_embeds=prompt_embeds,
@@ -73,9 +93,9 @@ def fill_image(image, model_selection):
73
  yield image, cnet_image
74
 
75
  image = image.convert("RGBA")
76
- cnet_image.paste(image, (0, 0), binary_mask)
77
 
78
- yield source, cnet_image
79
 
80
 
81
  def clear_result():
 
9
  from controlnet_union import ControlNetModel_Union
10
  from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
11
 
12
+ from PIL import Image, ImageDraw
13
+
14
  MODELS = {
15
  "RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
16
  }
 
57
 
58
  @spaces.GPU
59
  def fill_image(image, model_selection):
 
 
60
 
61
+ margin = 100
62
+ # Open the original image
63
+ source = image["image"] # Changed from image["background"] to match new input format
64
+
65
+ # Calculate new output size
66
+ output_size = (source.width + 2*margin, source.height + 2*margin)
67
+
68
+ # Create a white background
69
+ background = Image.new('RGB', output_size, (255, 255, 255))
70
+
71
+ # Calculate position to paste the original image
72
+ position = (margin, margin)
73
+
74
+ # Paste the original image onto the white background
75
+ background.paste(source, position)
76
+
77
+ # Create the mask
78
+ mask = Image.new('L', output_size, 255) # Start with all white
79
+ mask_draw = ImageDraw.Draw(mask)
80
+ mask_draw.rectangle([position, (position[0] + source.width, position[1] + source.height)], fill=0)
81
+
82
+ # Prepare the image for ControlNet
83
+ cnet_image = background.copy()
84
+ cnet_image.paste(0, (0, 0), mask)
85
 
86
  for image in pipe(
87
  prompt_embeds=prompt_embeds,
 
93
  yield image, cnet_image
94
 
95
  image = image.convert("RGBA")
96
+ cnet_image.paste(image, (0, 0), mask)
97
 
98
+ yield background, cnet_image
99
 
100
 
101
  def clear_result():