Spaces:
Runtime error
Runtime error
custom resize option uses percentages instead of fractions
Browse files52 unmodified lines535455565758593 unmodified lines636465666768697071727374757677787980818283848586196 unmodified lines28328428528628728828929029129229329429529629752 unmodified lines3503513523533543553564 unmodified lines36136236336436536636712 unmodified lines38038138238338438538652 unmodified linesreturn True@spaces.GPU(duration=24)def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_size, prompt_input=None, alignment="Middle"):source = imagetarget_size = (width, height)overlap = overlap_width3 unmodified linesnew_width = int(source.width * scale_factor)new_height = int(source.height * scale_factor)# Resize the source imagesource = source.resize((new_width, new_height), Image.LANCZOS)# Apply resize optionif resize_option == "Full":resize_size = max(source.width, source.height)elif resize_option == "1/2":resize_size = max(source.width, source.height) // 2elif resize_option == "1/3":resize_size = max(source.width, source.height) // 3elif resize_option == "1/4":resize_size = max(source.width, source.height) // 4else: # Customresize_size = custom_resize_sizeaspect_ratio = source.height / source.widthnew_width = resize_sizenew_height = int(resize_size * aspect_ratio)source = source.resize((new_width, new_height), Image.LANCZOS)# Calculate margins based on alignment196 unmodified lineswith gr.Row():resize_option = gr.Radio(label="Resize input image",choices=["Full", "1/2", "1/3", "1/4", "Custom"],value="Full")custom_resize_size = gr.Slider(label="Custom resize size",minimum=64,maximum=1024,step=8,value=512,visible=False)52 unmodified linesresize_option.change(fn=toggle_custom_resize_slider,inputs=[resize_option],outputs=[custom_resize_size],queue=False)4 unmodified lines).then( # Generate the new imagefn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,resize_option, custom_resize_size, prompt_input, alignment_dropdown],outputs=result,).then( # Update the history galleryfn=lambda x, history: update_history(x[1], history),12 unmodified lines).then( # Generate the new imagefn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,resize_option, custom_resize_size, prompt_input, alignment_dropdown],outputs=result,).then( # Update the history galleryfn=lambda x, history: update_history(x[1], history),52 unmodified lines535455565758593 unmodified lines63646566676869707172737475767778798081828384858687888990919293196 unmodified lines29029129229329429529629729829930030130230330452 unmodified lines3573583593603613623634 unmodified lines36836937037137237337412 unmodified lines38738838939039139239352 unmodified linesreturn 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_width3 unmodified linesnew_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":resize_percentage = 100elif resize_option == "50%":resize_percentage = 50elif resize_option == "33%":resize_percentage = 33elif resize_option == "25%":resize_percentage = 25else: # Customresize_percentage = custom_resize_percentage# Calculate new dimensions based on percentageresize_factor = resize_percentage / 100new_width = int(source.width * resize_factor)new_height = int(source.height * resize_factor)# Ensure minimum size of 64 pixelsnew_width = max(new_width, 64)new_height = max(new_height, 64)# Resize the imagesource = source.resize((new_width, new_height), Image.LANCZOS)# Calculate margins based on alignment196 unmodified lineswith gr.Row():resize_option = gr.Radio(label="Resize input image",choices=["Full", "50%", "33%", "25%", "Custom"],value="Full")custom_resize_percentage = gr.Slider(label="Custom resize percentage",minimum=1,maximum=100,step=1,value=50,visible=False)52 unmodified linesresize_option.change(fn=toggle_custom_resize_slider,inputs=[resize_option],outputs=[custom_resize_percentage],queue=False)4 unmodified lines).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),12 unmodified lines).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),
app.py+31-24