Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ 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",
|
@@ -107,33 +108,59 @@ def fill_image(image, model_selection):
|
|
107 |
def fill_image(image, model_selection):
|
108 |
source = image
|
109 |
target_ratio=(9, 16)
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
# Create a white background
|
122 |
-
background = Image.new('RGB',
|
123 |
|
124 |
-
#
|
125 |
position = (margin_x, margin_y)
|
|
|
126 |
|
127 |
-
#
|
128 |
-
|
|
|
129 |
|
130 |
-
# Create
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
# Prepare the image for ControlNet
|
139 |
cnet_image = background.copy()
|
|
|
10 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
11 |
|
12 |
from PIL import Image, ImageDraw
|
13 |
+
import numpy as np
|
14 |
|
15 |
MODELS = {
|
16 |
"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
|
|
|
108 |
def fill_image(image, model_selection):
|
109 |
source = image
|
110 |
target_ratio=(9, 16)
|
111 |
+
target_height=1280
|
112 |
+
overlap=48
|
113 |
+
fade_width=24
|
114 |
+
# Calculate target dimensions
|
115 |
+
target_width = (target_height * target_ratio[0]) // target_ratio[1]
|
116 |
|
117 |
+
# Resize the source image to fit within the target dimensions while maintaining aspect ratio
|
118 |
+
source_aspect = source.width / source.height
|
119 |
+
target_aspect = target_width / target_height
|
120 |
|
121 |
+
if source_aspect > target_aspect:
|
122 |
+
# Image is wider than target ratio, fit to width
|
123 |
+
new_width = target_width
|
124 |
+
new_height = int(new_width / source_aspect)
|
125 |
+
else:
|
126 |
+
# Image is taller than target ratio, fit to height
|
127 |
+
new_height = target_height
|
128 |
+
new_width = int(new_height * source_aspect)
|
129 |
+
|
130 |
+
resized_source = source.resize((new_width, new_height), Image.LANCZOS)
|
131 |
+
|
132 |
+
# Calculate margins
|
133 |
+
margin_x = (target_width - new_width) // 2
|
134 |
+
margin_y = (target_height - new_height) // 2
|
135 |
|
136 |
# Create a white background
|
137 |
+
background = Image.new('RGB', (target_width, target_height), (255, 255, 255))
|
138 |
|
139 |
+
# Paste the resized image onto the white background
|
140 |
position = (margin_x, margin_y)
|
141 |
+
background.paste(resized_source, position)
|
142 |
|
143 |
+
# Create the mask with gradient edges
|
144 |
+
mask = Image.new('L', (target_width, target_height), 255)
|
145 |
+
mask_array = np.array(mask)
|
146 |
|
147 |
+
# Create gradient for left and right edges
|
148 |
+
for i in range(fade_width):
|
149 |
+
alpha = i / fade_width
|
150 |
+
mask_array[:, margin_x+overlap+i] = np.minimum(mask_array[:, margin_x+overlap+i], int(255 * (1 - alpha)))
|
151 |
+
mask_array[:, margin_x+new_width-overlap-i-1] = np.minimum(mask_array[:, margin_x+new_width-overlap-i-1], int(255 * (1 - alpha)))
|
152 |
+
|
153 |
+
# Create gradient for top and bottom edges
|
154 |
+
for i in range(fade_width):
|
155 |
+
alpha = i / fade_width
|
156 |
+
mask_array[margin_y+overlap+i, :] = np.minimum(mask_array[margin_y+overlap+i, :], int(255 * (1 - alpha)))
|
157 |
+
mask_array[margin_y+new_height-overlap-i-1, :] = np.minimum(mask_array[margin_y+new_height-overlap-i-1, :], int(255 * (1 - alpha)))
|
158 |
+
|
159 |
+
# Set the center to black
|
160 |
+
mask_array[margin_y+overlap+fade_width:margin_y+new_height-overlap-fade_width,
|
161 |
+
margin_x+overlap+fade_width:margin_x+new_width-overlap-fade_width] = 0
|
162 |
+
|
163 |
+
mask = Image.fromarray(mask_array.astype('uint8'), 'L')
|
164 |
|
165 |
# Prepare the image for ControlNet
|
166 |
cnet_image = background.copy()
|