Spaces:
Runtime error
Runtime error
Update app.py
Browse files9 unmodified lines10111213141591 unmodified lines1071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381399 unmodified linesfrom pipeline_fill_sd_xl import StableDiffusionXLFillPipelinefrom PIL import Image, ImageDrawMODELS = {"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",91 unmodified linesdef fill_image(image, model_selection):source = imagetarget_ratio=(9, 16)overlap=24# Calculate the target width based on the 9:16 ratiotarget_width = (source.height * target_ratio[0]) // target_ratio[1]# Calculate marginsmargin_x = max(0, (target_width - source.width) // 2)margin_y = 0 # No vertical expansion# Calculate new output sizeoutput_size = (source.width + 2*margin_x, source.height + 2*margin_y)# Create a white backgroundbackground = Image.new('RGB', output_size, (255, 255, 255))# Calculate position to paste the original imageposition = (margin_x, 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([(position[0] + overlap, position[1] + overlap),(position[0] + source.width - overlap, position[1] + source.height - overlap)], fill=0)# Prepare the image for ControlNetcnet_image = background.copy()9 unmodified lines1011121314151691 unmodified lines1081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651669 unmodified linesfrom pipeline_fill_sd_xl import StableDiffusionXLFillPipelinefrom PIL import Image, ImageDrawimport numpy as npMODELS = {"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",91 unmodified linesdef fill_image(image, model_selection):source = imagetarget_ratio=(9, 16)target_height=1280overlap=48fade_width=24# 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:# Image is wider than target ratio, fit to widthnew_width = target_widthnew_height = int(new_width / source_aspect)else:# Image is taller than target ratio, fit to heightnew_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 mask with gradient edgesmask = Image.new('L', (target_width, target_height), 255)mask_array = np.array(mask)# Create gradient for left and right edgesfor i in range(fade_width):alpha = i / fade_widthmask_array[:, margin_x+overlap+i] = np.minimum(mask_array[:, margin_x+overlap+i], int(255 * (1 - alpha)))mask_array[:, margin_x+new_width-overlap-i-1] = np.minimum(mask_array[:, margin_x+new_width-overlap-i-1], int(255 * (1 - alpha)))# Create gradient for top and bottom edgesfor i in range(fade_width):alpha = i / fade_widthmask_array[margin_y+overlap+i, :] = np.minimum(mask_array[margin_y+overlap+i, :], int(255 * (1 - alpha)))mask_array[margin_y+new_height-overlap-i-1, :] = np.minimum(mask_array[margin_y+new_height-overlap-i-1, :], int(255 * (1 - alpha)))# Set the center to blackmask_array[margin_y+overlap+fade_width:margin_y+new_height-overlap-fade_width,margin_x+overlap+fade_width:margin_x+new_width-overlap-fade_width] = 0mask = Image.fromarray(mask_array.astype('uint8'), 'L')# Prepare the image for ControlNetcnet_image = background.copy()
app.py+46-19