fffiloni commited on
Commit
d49f90c
1 Parent(s): 0e8df3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -19
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
- overlap=24
111
- # Calculate the target width based on the 9:16 ratio
112
- target_width = (source.height * target_ratio[0]) // target_ratio[1]
 
 
113
 
114
- # Calculate margins
115
- margin_x = max(0, (target_width - source.width) // 2)
116
- margin_y = 0 # No vertical expansion
117
 
118
- # Calculate new output size
119
- output_size = (source.width + 2*margin_x, source.height + 2*margin_y)
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Create a white background
122
- background = Image.new('RGB', output_size, (255, 255, 255))
123
 
124
- # Calculate position to paste the original image
125
  position = (margin_x, margin_y)
 
126
 
127
- # Paste the original image onto the white background
128
- background.paste(source, position)
 
129
 
130
- # Create the mask
131
- mask = Image.new('L', output_size, 255) # Start with all white
132
- mask_draw = ImageDraw.Draw(mask)
133
- mask_draw.rectangle([
134
- (position[0] + overlap, position[1] + overlap),
135
- (position[0] + source.width - overlap, position[1] + source.height - overlap)
136
- ], fill=0)
 
 
 
 
 
 
 
 
 
 
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()