Spaces:
Runtime error
Runtime error
Update app.py
Browse files110 unmodified lines111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163110 unmodified linestarget_height=1280overlap=48fade_width=24# Calculate target dimensionstarget_width = (target_height * target_ratio[0]) // target_ratio[1]# Resize the source image to fit the target widthnew_width = target_widthnew_height = int(source.height * (new_width / source.width))resized_source = source.resize((new_width, new_height), Image.LANCZOS)# Create a white backgroundbackground = Image.new('RGB', (target_width, target_height), (255, 255, 255))# Calculate position to paste the resized image (centered vertically)margin_y = (target_height - new_height) // 2position = (0, margin_y)# Paste the resized image onto the white backgroundbackground.paste(resized_source, position)# Create the maskmask = Image.new('L', (target_width, target_height), 255) # Start with all whitemask_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)], fill=0)# Convert mask to numpy array for gradient creationmask_array = np.array(mask)# Create gradient on the edges that overlap with the imagefor i in range(fade_width):alpha = i / fade_width# Top edgeif margin_y - overlap + i < margin_y:mask_array[margin_y - overlap + i, :new_width] = int(255 * alpha)# Bottom edgeif 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 edgemask_array[margin_y:margin_y+new_height, i] = int(255 * alpha)# Right edgemask_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 ControlNetcnet_image = background.copy()cnet_image.paste(0, (0, 0), mask)110 unmodified lines111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142110 unmodified linestarget_height=1280overlap=48fade_width=24# Calculate the required height for 9:16 ratiotarget_height = (source.width * target_ratio[1]) // target_ratio[0]# Calculate margins (only top and bottom)margin_y = (target_height - source.height) // 2# Calculate new output sizeoutput_size = (source.width, target_height)# Create a white backgroundbackground = Image.new('RGB', output_size, (255, 255, 255))# Calculate position to paste the original imageposition = (0, margin_y)# Paste the original image onto the white backgroundbackground.paste(source, position)# Create the maskmask = Image.new('L', output_size, 255) # Start with all whitemask_draw = ImageDraw.Draw(mask)mask_draw.rectangle([(overlap, margin_y + overlap),(source.width - overlap, margin_y + source.height - overlap)], fill=0)# Prepare the image for ControlNetcnet_image = background.copy()cnet_image.paste(0, (0, 0), mask)
app.py+14-35