Skip to content

Commit

Permalink
autoset width and height (#1838)
Browse files Browse the repository at this point in the history
* autoset width and height

When loading into img2img, added an option to autoset Width and Height from image (Settings -> img2img ->After loading into Img2img, automatically update Width and Height).
Also fixed scale_by display, now correctly rounded to multiple of 8
Also fixed img2img new width/height calculation, same way, (affects infotext)

* Update ui.py: hide progress animation on image size change
  • Loading branch information
DenOfEquity committed Sep 16, 2024
1 parent 210af4f commit 791f04f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions modules/img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ def img2img_function(id_task: str, request: gr.Request, mode: int, prompt: str,
assert image, "Can't scale by because no image is selected"

width = int(image.width * scale_by)
width -= width % 8
height = int(image.height * scale_by)
height -= height % 8

assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'

Expand Down
2 changes: 2 additions & 0 deletions modules/shared_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@
"return_mask_composite": OptionInfo(False, "For inpainting, include masked composite in results for web"),
"img2img_batch_show_results_limit": OptionInfo(32, "Show the first N batch img2img results in UI", gr.Slider, {"minimum": -1, "maximum": 1000, "step": 1}).info('0: disable, -1: show all images. Too many images can cause lag'),
"overlay_inpaint": OptionInfo(True, "Overlay original for inpaint").info("when inpainting, overlay the original image over the areas that weren't inpainted."),
"img2img_autosize": OptionInfo(False, "After loading into Img2img, automatically update Width and Height"),

}))

options_templates.update(options_section(('optimizations', "Optimizations", "sd"), {
Expand Down
18 changes: 16 additions & 2 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def resize_from_to_html(width, height, scale_by):
if not target_width or not target_height:
return "no image selected"

target_width -= target_width % 8 # note: hardcoded latent size 8
target_height -= target_height % 8

return f"resize: from <span class='resolution'>{width}x{height}</span> to <span class='resolution'>{target_width}x{target_height}</span>"


Expand Down Expand Up @@ -636,7 +639,7 @@ def copyCanvas_img2img (background, foreground, source):
detect_image_size_btn = ToolButton(value=detect_image_size_symbol, elem_id="img2img_detect_image_size_btn", tooltip="Auto detect size from img2img")

with gr.Tab(label="Resize by", id="by", elem_id="img2img_tab_resize_by") as tab_scale_by:
scale_by = gr.Slider(minimum=0.05, maximum=4.0, step=0.05, label="Scale", value=1.0, elem_id="img2img_scale")
scale_by = gr.Slider(minimum=0.05, maximum=4.0, step=0.01, label="Scale", value=1.0, elem_id="img2img_scale")

with FormRow():
scale_by_html = FormHTML(resize_from_to_html(0, 0, 0.0), elem_id="img2img_scale_resolution_preview")
Expand All @@ -651,9 +654,20 @@ def copyCanvas_img2img (background, foreground, source):
show_progress=False,
)

scale_by.release(**on_change_args)
scale_by.change(**on_change_args)
button_update_resize_to.click(**on_change_args)

def updateWH (img, w, h):
if img and shared.opts.img2img_autosize == True:
return img.size[0], img.size[1]
else:
return w, h

img_sources = [init_img.background, sketch.background, init_img_with_mask.background, inpaint_color_sketch.background, init_img_inpaint]
for i in img_sources:
i.change(fn=updateWH, inputs=[i, width, height], outputs=[width, height], show_progress='hidden')
i.change(**on_change_args)

tab_scale_to.select(fn=lambda: 0, inputs=[], outputs=[selected_scale_tab])
tab_scale_by.select(fn=lambda: 1, inputs=[], outputs=[selected_scale_tab])

Expand Down

0 comments on commit 791f04f

Please sign in to comment.