Spaces:
Runtime error
Runtime error
Update app.py
Browse files110 unmodified lines111112113114115114115116117118119120117118119120121122123123124125125126126127128129130129130131132133133134135136135138139136137138139142143144145146147148149150151152153154155156157158159160140141142110 unmodified lines target_height=1280 overlap=48 fade_width=24 # Calculate target dimensions target_width = (target_height * target_ratio[0]) // target_ratio[1] # Calculate the required height for 9:16 ratio target_height = (source.width * target_ratio[1]) // target_ratio[0] # Resize the source image to fit the target width new_width = target_width new_height = int(source.height * (new_width / source.width)) resized_source = source.resize((new_width, new_height), Image.LANCZOS) # Calculate margins (only top and bottom) margin_y = (target_height - source.height) // 2 # Calculate new output size output_size = (source.width, target_height) # Create a white background background = Image.new('RGB', (target_width, target_height), (255, 255, 255)) background = Image.new('RGB', output_size, (255, 255, 255)) # Calculate position to paste the resized image (centered vertically) margin_y = (target_height - new_height) // 2 # Calculate position to paste the original image position = (0, margin_y) # Paste the resized image onto the white background background.paste(resized_source, position) # Paste the original image onto the white background background.paste(source, position) # Create the mask mask = Image.new('L', (target_width, target_height), 255) # Start with all white mask = Image.new('L', output_size, 255) # Start with all white mask_draw = ImageDraw.Draw(mask) # Draw black rectangle for the resized image area (with overlap) mask_draw.rectangle([ (-overlap, margin_y - overlap), (new_width + overlap, margin_y + new_height + overlap) (overlap, margin_y + overlap), (source.width - overlap, margin_y + source.height - overlap) ], fill=0) # Convert mask to numpy array for gradient creation mask_array = np.array(mask) # Create gradient on the edges that overlap with the image for i in range(fade_width): alpha = i / fade_width # Top edge if margin_y - overlap + i < margin_y: mask_array[margin_y - overlap + i, :new_width] = int(255 * alpha) # Bottom edge if margin_y + new_height + overlap - i - 1 >= margin_y + new_height: mask_array[margin_y + new_height + overlap - i - 1, :new_width] = int(255 * alpha) # Left edge mask_array[margin_y:margin_y+new_height, i] = int(255 * alpha) # Right edge mask_array[margin_y:margin_y+new_height, new_width - i - 1] = int(255 * alpha) mask = Image.fromarray(mask_array.astype('uint8'), 'L') # Prepare the image for ControlNet cnet_image = background.copy() cnet_image.paste(0, (0, 0), mask)app.py+14-35