Spaces:
Configuration error
Configuration error
multiple changes, new preview mask and alignement
Browse files51 unmodified lines5253545556575859606162636465666768697019 unmodified lines90919293949511 unmodified lines1071081091101111122 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714832 unmodified lines18118218318418518618718818919019119219319419569 unmodified lines2652662672682692702712722732742752762772782792802812822832842852862872882892902912921 unmodified line2942952962972982993003013023033043053065 unmodified lines3123133143153163172 unmodified lines32032132232332432541 unmodified lines36736836937037137237337411 unmodified lines3863873883893903913923935 unmodified lines39940040140251 unmodified linesreturn Falsereturn True@spaces.GPU(duration=24)def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_percentage, prompt_input=None, alignment="Middle"):source = imagetarget_size = (width, height)overlap = overlap_width# Calculate the scaling factor to fit the image within the target sizescale_factor = min(target_size[0] / source.width, target_size[1] / source.height)new_width = int(source.width * scale_factor)new_height = int(source.height * scale_factor)# Resize the source image to fit within target sizesource = source.resize((new_width, new_height), Image.LANCZOS)# Apply resize option using percentagesif resize_option == "Full":19 unmodified lines# Resize the imagesource = source.resize((new_width, new_height), Image.LANCZOS)# Calculate margins based on alignmentif alignment == "Middle":margin_x = (target_size[0] - new_width) // 211 unmodified linesmargin_x = (target_size[0] - new_width) // 2margin_y = target_size[1] - new_height# Create a new background image and paste the resized source imagebackground = Image.new('RGB', target_size, (255, 255, 255))background.paste(source, (margin_x, margin_y))2 unmodified linesmask = Image.new('L', target_size, 255)mask_draw = ImageDraw.Draw(mask)# Adjust mask generation based on alignmentif alignment == "Middle":mask_draw.rectangle([(margin_x + overlap, margin_y + overlap),(margin_x + new_width - overlap, margin_y + new_height - overlap)], fill=0)elif alignment == "Left":mask_draw.rectangle([(margin_x, margin_y),(margin_x + new_width - overlap, margin_y + new_height)], fill=0)elif alignment == "Right":mask_draw.rectangle([(margin_x + overlap, margin_y),(margin_x + new_width, margin_y + new_height)], fill=0)elif alignment == "Top":mask_draw.rectangle([(margin_x, margin_y),(margin_x + new_width, margin_y + new_height - overlap)], fill=0)elif alignment == "Bottom":mask_draw.rectangle([(margin_x, margin_y + overlap),(margin_x + new_width, margin_y + new_height)], fill=0)if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):alignment = "Middle"cnet_image = background.copy()32 unmodified linesif target_ratio == "9:16":changed_width = 720changed_height = 1280return changed_width, changed_height, gr.update(open=False)elif target_ratio == "16:9":changed_width = 1280changed_height = 720return changed_width, changed_height, gr.update(open=False)elif target_ratio == "1:1":changed_width = 1024changed_height = 1024return changed_width, changed_height, gr.update(open=False)elif target_ratio == "Custom":return ui_width, ui_height, gr.update(open=True)69 unmodified lineswith gr.Column():with gr.Row():width_slider = gr.Slider(label="Width",minimum=720,maximum=1536,step=8,value=720, # Set a default value)height_slider = gr.Slider(label="Height",minimum=720,maximum=1536,step=8,value=1280, # Set a default value)with gr.Row():num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)overlap_width = gr.Slider(label="Mask overlap width",minimum=1,maximum=50,value=42,step=1)with gr.Row():resize_option = gr.Radio(label="Resize input image",1 unmodified linevalue="Full")custom_resize_percentage = gr.Slider(label="Custom resize percentage",minimum=1,maximum=100,step=1,value=50,visible=False)gr.Examples(examples=[5 unmodified linesinputs=[input_image, width_slider, height_slider, alignment_dropdown],)with gr.Column():result = ImageSlider(interactive=False,2 unmodified linesuse_as_input_button = gr.Button("Use as Input Image", visible=False)history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)41 unmodified linesoutputs=result,).then( # Generate the new imagefn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,resize_option, custom_resize_percentage, prompt_input, alignment_dropdown],outputs=result,).then( # Update the history galleryfn=lambda x, history: update_history(x[1], history),11 unmodified linesoutputs=result,).then( # Generate the new imagefn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,resize_option, custom_resize_percentage, prompt_input, alignment_dropdown],outputs=result,).then( # Update the history galleryfn=lambda x, history: update_history(x[1], history),5 unmodified linesoutputs=use_as_input_button,)demo.queue(max_size=12).launch(share=False)51 unmodified lines5253545556575859606162636465666719 unmodified lines8788899091929394959697989910011 unmodified lines1121131141151161171181191201212 unmodified lines12412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516632 unmodified lines19920020120220320420520620720820921021121221369 unmodified lines2832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163171 unmodified line3193203213223233243253263273283293303313323333343355 unmodified lines3413423433443453463473482 unmodified lines35135235335435535635741 unmodified lines39940040140240340440540640711 unmodified lines4194204214224234244254264275 unmodified lines43343443543643743843944044144244344451 unmodified linesreturn Falsereturn Truedef prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):target_size = (width, height)# Calculate the scaling factor to fit the image within the target sizescale_factor = min(target_size[0] / image.width, target_size[1] / image.height)new_width = int(image.width * scale_factor)new_height = int(image.height * scale_factor)# Resize the source image to fit within target sizesource = image.resize((new_width, new_height), Image.LANCZOS)# Apply resize option using percentagesif resize_option == "Full":19 unmodified lines# Resize the imagesource = source.resize((new_width, new_height), Image.LANCZOS)# Calculate the overlap in pixels based on the percentageoverlap_x = int(new_width * (overlap_percentage / 100))overlap_y = int(new_height * (overlap_percentage / 100))# Ensure minimum overlap of 1 pixeloverlap_x = max(overlap_x, 1)overlap_y = max(overlap_y, 1)# Calculate margins based on alignmentif alignment == "Middle":margin_x = (target_size[0] - new_width) // 211 unmodified linesmargin_x = (target_size[0] - new_width) // 2margin_y = target_size[1] - new_height# Adjust margins to eliminate gapsmargin_x = max(0, min(margin_x, target_size[0] - new_width))margin_y = max(0, min(margin_y, target_size[1] - new_height))# Create a new background image and paste the resized source imagebackground = Image.new('RGB', target_size, (255, 255, 255))background.paste(source, (margin_x, margin_y))2 unmodified linesmask = Image.new('L', target_size, 255)mask_draw = ImageDraw.Draw(mask)# Calculate overlap areasleft_overlap = margin_x + overlap_x if overlap_left else margin_xright_overlap = margin_x + new_width - overlap_x if overlap_right else margin_x + new_widthtop_overlap = margin_y + overlap_y if overlap_top else margin_ybottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height# Draw the maskmask_draw.rectangle([(left_overlap, top_overlap),(right_overlap, bottom_overlap)], fill=0)return background, maskdef preview_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)# Create a preview image showing the maskpreview = background.copy().convert('RGBA')# Create a semi-transparent red overlayred_overlay = Image.new('RGBA', background.size, (255, 0, 0, 64)) # Reduced alpha to 64 (25% opacity)# Convert black pixels in the mask to semi-transparent redred_mask = Image.new('RGBA', background.size, (0, 0, 0, 0))red_mask.paste(red_overlay, (0, 0), mask)# Overlay the red mask on the backgroundpreview = Image.alpha_composite(preview, red_mask)return preview@spaces.GPU(duration=24)def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)if not can_expand(background.width, background.height, width, height, alignment):alignment = "Middle"cnet_image = background.copy()32 unmodified linesif target_ratio == "9:16":changed_width = 720changed_height = 1280return changed_width, changed_height, gr.update()elif target_ratio == "16:9":changed_width = 1280changed_height = 720return changed_width, changed_height, gr.update()elif target_ratio == "1:1":changed_width = 1024changed_height = 1024return changed_width, changed_height, gr.update()elif target_ratio == "Custom":return ui_width, ui_height, gr.update(open=True)69 unmodified lineswith gr.Column():with gr.Row():width_slider = gr.Slider(label="Target Width",minimum=720,maximum=1536,step=8,value=720, # Set a default value)height_slider = gr.Slider(label="Target Height",minimum=720,maximum=1536,step=8,value=1280, # Set a default value)num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)with gr.Group():overlap_percentage = gr.Slider(label="Mask overlap (%)",minimum=1,maximum=50,value=10,step=1)with gr.Row():overlap_top = gr.Checkbox(label="Overlap Top", value=True)overlap_right = gr.Checkbox(label="Overlap Right", value=True)with gr.Row():overlap_left = gr.Checkbox(label="Overlap Left", value=True)overlap_bottom = gr.Checkbox(label="Overlap Bottom", value=True)with gr.Row():resize_option = gr.Radio(label="Resize input image",1 unmodified linevalue="Full")custom_resize_percentage = gr.Slider(label="Custom resize (%)",minimum=1,maximum=100,step=1,value=50,visible=False)with gr.Column():preview_button = gr.Button("Preview alignment and mask")gr.Examples(examples=[5 unmodified linesinputs=[input_image, width_slider, height_slider, alignment_dropdown],)with gr.Column():result = ImageSlider(interactive=False,2 unmodified linesuse_as_input_button = gr.Button("Use as Input Image", visible=False)history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)preview_image = gr.Image(label="Preview")41 unmodified linesoutputs=result,).then( # Generate the new imagefn=infer,inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,overlap_left, overlap_right, overlap_top, overlap_bottom],outputs=result,).then( # Update the history galleryfn=lambda x, history: update_history(x[1], history),11 unmodified linesoutputs=result,).then( # Generate the new imagefn=infer,inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,overlap_left, overlap_right, overlap_top, overlap_bottom],outputs=result,).then( # Update the history galleryfn=lambda x, history: update_history(x[1], history),5 unmodified linesoutputs=use_as_input_button,)preview_button.click(fn=preview_image_and_mask,inputs=[input_image, width_slider, height_slider, overlap_percentage, resize_option, custom_resize_percentage, alignment_dropdown,overlap_left, overlap_right, overlap_top, overlap_bottom],outputs=preview_image,queue=False)demo.queue(max_size=12).launch(share=False)
app.py+92-50