diff --git a/modules/shared.py b/modules/shared.py index 79fbf724..7d8116fb 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -139,6 +139,15 @@ ui_reorder_categories = [ "scripts", ] +aspect_ratio_defaults = [ + "1:1", + "1:2", + "3:2", + "4:3", + "16:9", +] + + cmd_opts.disable_extension_access = (cmd_opts.share or cmd_opts.listen or cmd_opts.server_name) and not cmd_opts.enable_insecure_extension_access devices.device, devices.device_interrogate, devices.device_gfpgan, devices.device_esrgan, devices.device_codeformer = \ @@ -456,6 +465,7 @@ options_templates.update(options_section(('ui', "User interface"), { "keyedit_precision_extra": OptionInfo(0.05, "Ctrl+up/down precision when editing ", gr.Slider, {"minimum": 0.01, "maximum": 0.2, "step": 0.001}), "quicksettings": OptionInfo("sd_model_checkpoint", "Quicksettings list"), "ui_reorder": OptionInfo(", ".join(ui_reorder_categories), "txt2img/img2img UI item order"), + "aspect_ratios": OptionInfo(", ".join(aspect_ratio_defaults), "txt2img/img2img aspect ratios"), "ui_extra_networks_tab_reorder": OptionInfo("", "Extra networks tab order"), "localization": OptionInfo("None", "Localization (requires restart)", gr.Dropdown, lambda: {"choices": ["None"] + list(localization.localizations.keys())}, refresh=lambda: localization.list_localizations(cmd_opts.localizations_dir)), })) diff --git a/modules/ui.py b/modules/ui.py index f5df1ffe..b2964768 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -424,6 +424,20 @@ def ordered_ui_categories(): yield category +def aspect_ratio_list(): + return [ratio.strip() for ratio in shared.opts.aspect_ratios.split(",")] + + +def aspect_ratio_resize(w, h, bttn_val): + ratio = reduce(lambda bttn_val_width, bttn_val_height: int(bttn_val_width) / int(bttn_val_height), bttn_val.split(":")) + if ratio < 1: + return (round(h * ratio), h) + elif ratio > 1: + return (w, round(w * ratio)) + else: + return [min(w,h)] * 2 + + def get_value_for_setting(key): value = getattr(opts, key) @@ -478,6 +492,10 @@ def create_ui(): with gr.Column(elem_id="txt2img_column_size", scale=4): width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="txt2img_width") height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="txt2img_height") + with gr.Row(): + for aspect_ratio in aspect_ratio_list(): + aspect_ratio_bttn = ToolButton(value=aspect_ratio, elem_id=f"txt2img_ratio_{aspect_ratio.replace(':', '_')}") + aspect_ratio_bttn.click(aspect_ratio_resize, inputs=[width, height, aspect_ratio_bttn], outputs=[width, height]) res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="txt2img_res_switch_btn") if opts.dimensions_and_batch_together: @@ -756,6 +774,11 @@ def create_ui(): with gr.Column(elem_id="img2img_column_size", scale=4): width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="img2img_width") height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="img2img_height") + with gr.Row(): + for aspect_ratio in aspect_ratio_list(): + aspect_ratio_bttn = ToolButton(value=aspect_ratio, elem_id=f"img2img_ratio_{aspect_ratio.replace(':', '_')}") + aspect_ratio_bttn.click(aspect_ratio_resize, inputs=[width, height, aspect_ratio_bttn], outputs=[width, height]) + res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="img2img_res_switch_btn") if opts.dimensions_and_batch_together: