From d18eb10ecdbf3690571d2d7ce0c0fe548d00d836 Mon Sep 17 00:00:00 2001 From: Haoming Date: Wed, 13 Mar 2024 21:15:52 +0800 Subject: [PATCH 1/3] add hook --- modules/ui_postprocessing.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/modules/ui_postprocessing.py b/modules/ui_postprocessing.py index 7261c2df8..e9d82d46a 100644 --- a/modules/ui_postprocessing.py +++ b/modules/ui_postprocessing.py @@ -4,6 +4,30 @@ import modules.infotext_utils as parameters_copypaste from modules.ui_components import ResizeHandleRow +def hook_scale_update(inputs): + resize = upscaler = None + for script in inputs: + if script.label == "Resize": + resize = script + elif script.label == "Upscaler 1": + upscaler = script + elif resize and upscaler: + break + + def update_scale(upscaler: str, slider: float): + if upscaler[1] in ('x', 'X'): + try: + scale = int(upscaler[0]) + return gr.update(value=scale) + except ValueError: + return gr.update(value=slider) + + return gr.update(value=slider) + + if resize and upscaler: + upscaler.input(update_scale, inputs=[upscaler, resize], outputs=[resize]) + + def create_ui(): dummy_component = gr.Label(visible=False) tab_index = gr.Number(value=0, visible=False) @@ -23,6 +47,7 @@ def create_ui(): show_extras_results = gr.Checkbox(label='Show result images', value=True, elem_id="extras_show_extras_results") script_inputs = scripts.scripts_postproc.setup_ui() + hook_scale_update(script_inputs) with gr.Column(): toprow = ui_toprow.Toprow(is_compact=True, is_img2img=False, id_part="extras") From fd71b761ff6b6636204ffcd71212cdbb7bb5d658 Mon Sep 17 00:00:00 2001 From: Haoming Date: Thu, 14 Mar 2024 09:55:14 +0800 Subject: [PATCH 2/3] use re instead of hardcoding Now supports all natively provided upscaler as well --- modules/ui_postprocessing.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/ui_postprocessing.py b/modules/ui_postprocessing.py index e9d82d46a..7fd889105 100644 --- a/modules/ui_postprocessing.py +++ b/modules/ui_postprocessing.py @@ -5,6 +5,9 @@ from modules.ui_components import ResizeHandleRow def hook_scale_update(inputs): + import re + pattern = r'(\d)[xX]|[xX](\d)' + resize = upscaler = None for script in inputs: if script.label == "Resize": @@ -15,14 +18,17 @@ def hook_scale_update(inputs): break def update_scale(upscaler: str, slider: float): - if upscaler[1] in ('x', 'X'): - try: - scale = int(upscaler[0]) - return gr.update(value=scale) - except ValueError: - return gr.update(value=slider) + match = re.search(pattern, upscaler) - return gr.update(value=slider) + if match: + if match.group(1): + return gr.update(value=int(match.group(1))) + + else: + return gr.update(value=int(match.group(2))) + + else: + return gr.update(value=slider) if resize and upscaler: upscaler.input(update_scale, inputs=[upscaler, resize], outputs=[resize]) From 4e17fc36d87a1d983c542dbfb606a93e70a68e2a Mon Sep 17 00:00:00 2001 From: Haoming Date: Thu, 14 Mar 2024 10:04:09 +0800 Subject: [PATCH 3/3] add user setting Now this is disabled by default --- modules/shared_options.py | 1 + modules/ui_postprocessing.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/shared_options.py b/modules/shared_options.py index 21643afe0..99a051aaf 100644 --- a/modules/shared_options.py +++ b/modules/shared_options.py @@ -101,6 +101,7 @@ options_templates.update(options_section(('upscaling', "Upscaling", "postprocess "DAT_tile": OptionInfo(192, "Tile size for DAT upscalers.", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}).info("0 = no tiling"), "DAT_tile_overlap": OptionInfo(8, "Tile overlap for DAT upscalers.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}).info("Low values = visible seam"), "upscaler_for_img2img": OptionInfo(None, "Upscaler for img2img", gr.Dropdown, lambda: {"choices": [x.name for x in shared.sd_upscalers]}), + "scaleBy_from_upscaler": OptionInfo(False, "Automatically set the Scale by factor based on the name of the selected Upscaler.").info("Will not change the value when no matching pattern is found."), })) options_templates.update(options_section(('face-restoration', "Face restoration", "postprocessing"), { diff --git a/modules/ui_postprocessing.py b/modules/ui_postprocessing.py index 7fd889105..af44f6195 100644 --- a/modules/ui_postprocessing.py +++ b/modules/ui_postprocessing.py @@ -53,7 +53,8 @@ def create_ui(): show_extras_results = gr.Checkbox(label='Show result images', value=True, elem_id="extras_show_extras_results") script_inputs = scripts.scripts_postproc.setup_ui() - hook_scale_update(script_inputs) + if getattr(shared.opts, 'scaleBy_from_upscaler', False): + hook_scale_update(script_inputs) with gr.Column(): toprow = ui_toprow.Toprow(is_compact=True, is_img2img=False, id_part="extras")