Spaces:
Configuration error
Configuration error
Update app.py
Browse files113 unmodified lines114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165113 unmodified lines# Calculate target dimensionstarget_width = (target_height * target_ratio[0]) // target_ratio[1]# Resize the source image to fit within the target dimensions while maintaining aspect ratiosource_aspect = source.width / source.heighttarget_aspect = target_width / target_heightif source_aspect > target_aspect:new_width = target_widthnew_height = int(new_width / source_aspect)else:new_height = target_heightnew_width = int(new_height * source_aspect)resized_source = source.resize((new_width, new_height), Image.LANCZOS)# Calculate marginsmargin_x = (target_width - new_width) // 2margin_y = (target_height - new_height) // 2# Create a white backgroundbackground = Image.new('RGB', (target_width, target_height), (255, 255, 255))# Paste the resized image onto the white backgroundposition = (margin_x, margin_y)background.paste(resized_source, position)# Create the maskmask = Image.new('L', (target_width, target_height), 255) # Start with all whitemask_array = np.array(mask)# Create gradient only at the edges adjacent to the original imagefor i in range(fade_width):alpha = i / fade_width# Right edgeif margin_x + new_width + i < target_width:mask_array[:, margin_x + new_width + i] = np.minimum(mask_array[:, margin_x + new_width + i], int(255 * alpha))# Left edgeif margin_x - i - 1 >= 0:mask_array[:, margin_x - i - 1] = np.minimum(mask_array[:, margin_x - i - 1], int(255 * alpha))# Bottom edgeif margin_y + new_height + i < target_height:mask_array[margin_y + new_height + i, :] = np.minimum(mask_array[margin_y + new_height + i, :], int(255 * alpha))# Top edgeif margin_y - i - 1 >= 0:mask_array[margin_y - i - 1, :] = np.minimum(mask_array[margin_y - i - 1, :], int(255 * alpha))# Set the area of the original image to black (0)mask_array[margin_y:margin_y+new_height, margin_x:margin_x+new_width] = 0mask = Image.fromarray(mask_array.astype('uint8'), 'L')113 unmodified lines114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160113 unmodified lines# 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')
app.py+26-31