Commit 68999d0b authored by space-nuko's avatar space-nuko
Browse files

Add upscale slider to img2img

parent 91ae48fd
Loading
Loading
Loading
Loading
+23 −2
Original line number Original line Diff line number Diff line
@@ -8,8 +8,8 @@ function set_theme(theme){
}
}


function selected_gallery_index(){
function selected_gallery_index(){
    var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery] .gallery-item')
    var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery] .thumbnails > .thumbnail-item')
    var button = gradioApp().querySelector('[style="display: block;"].tabitem div[id$=_gallery] .gallery-item.\\!ring-2')
    var button = gradioApp().querySelector('[style="display: block;"].tabitem div[id$=_gallery] .thumbnails > .thumbnail-item.selected')


    var result = -1
    var result = -1
    buttons.forEach(function(v, i){ if(v==button) { result = i } })
    buttons.forEach(function(v, i){ if(v==button) { result = i } })
@@ -111,6 +111,14 @@ function get_img2img_tab_index() {
    return res
    return res
}
}


function get_img2img_tab_index_for_res_preview() {
    let res = args_to_array(arguments)
    res.splice(-1) // gradio also sends outputs to the arguments, pop them off
    res[0] = get_tab_index('mode_img2img')
    debugger;
    return res
}

function create_submit_args(args){
function create_submit_args(args){
    res = []
    res = []
    for(var i=0;i<args.length;i++){
    for(var i=0;i<args.length;i++){
@@ -335,3 +343,16 @@ function selectCheckpoint(name){
    desiredCheckpointName = name;
    desiredCheckpointName = name;
    gradioApp().getElementById('change_checkpoint').click()
    gradioApp().getElementById('change_checkpoint').click()
}
}


function onCalcResolutionImg2Img(init_img, scale, width, height, resize_mode){
    i2iScale = gradioApp().getElementById('img2img_scale')
    i2iWidth = gradioApp().getElementById('img2img_width')
    i2iHeight = gradioApp().getElementById('img2img_height')

    setInactive(i2iScale, scale == 1)
    setInactive(i2iWidth, scale > 1)
    setInactive(i2iHeight, scale > 1)

    return [init_img, width, height, scale, resize_mode]
}
+3 −0
Original line number Original line Diff line number Diff line
@@ -282,6 +282,9 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model
        res["Hires resize-1"] = 0
        res["Hires resize-1"] = 0
        res["Hires resize-2"] = 0
        res["Hires resize-2"] = 0


    if "Img2Img Upscale" not in res:
        res["Img2Img Upscale"] = 1

    restore_old_hires_fix_params(res)
    restore_old_hires_fix_params(res)


    return res
    return res
+2 −1
Original line number Original line Diff line number Diff line
@@ -78,7 +78,7 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args):
                processed_image.save(os.path.join(output_dir, filename))
                processed_image.save(os.path.join(output_dir, filename))




def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_index: int, mask_blur: int, mask_alpha: float, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, *args):
def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_index: int, mask_blur: int, mask_alpha: float, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, scale: float, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, *args):
    override_settings = create_override_settings_dict(override_settings_texts)
    override_settings = create_override_settings_dict(override_settings_texts)


    is_batch = mode == 5
    is_batch = mode == 5
@@ -149,6 +149,7 @@ def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_s
        inpaint_full_res_padding=inpaint_full_res_padding,
        inpaint_full_res_padding=inpaint_full_res_padding,
        inpainting_mask_invert=inpainting_mask_invert,
        inpainting_mask_invert=inpainting_mask_invert,
        override_settings=override_settings,
        override_settings=override_settings,
        scale=scale,
    )
    )


    p.scripts = modules.scripts.scripts_txt2img
    p.scripts = modules.scripts.scripts_txt2img
+17 −1
Original line number Original line Diff line number Diff line
@@ -929,7 +929,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
    sampler = None
    sampler = None


    def __init__(self, init_images: list = None, resize_mode: int = 0, denoising_strength: float = 0.75, image_cfg_scale: float = None, mask: Any = None, mask_blur: int = 4, inpainting_fill: int = 0, inpaint_full_res: bool = True, inpaint_full_res_padding: int = 0, inpainting_mask_invert: int = 0, initial_noise_multiplier: float = None, **kwargs):
    def __init__(self, init_images: Optional[list] = None, resize_mode: int = 0, denoising_strength: float = 0.75, image_cfg_scale: Optional[float] = None, mask: Any = None, mask_blur: int = 4, inpainting_fill: int = 0, inpaint_full_res: bool = True, inpaint_full_res_padding: int = 0, inpainting_mask_invert: int = 0, initial_noise_multiplier: Optional[float] = None, scale: float = 0, **kwargs):
        super().__init__(**kwargs)
        super().__init__(**kwargs)


        self.init_images = init_images
        self.init_images = init_images
@@ -949,11 +949,27 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
        self.mask = None
        self.mask = None
        self.nmask = None
        self.nmask = None
        self.image_conditioning = None
        self.image_conditioning = None
        self.scale = scale

    def get_final_size(self):
        if self.scale > 1:
            img = self.init_images[0]
            width = int(img.width * self.scale)
            height = int(img.height * self.scale)
            return width, height
        else:
            return self.width, self.height



    def init(self, all_prompts, all_seeds, all_subseeds):
    def init(self, all_prompts, all_seeds, all_subseeds):
        self.sampler = sd_samplers.create_sampler(self.sampler_name, self.sd_model)
        self.sampler = sd_samplers.create_sampler(self.sampler_name, self.sd_model)
        crop_region = None
        crop_region = None


        if self.scale > 1:
            self.extra_generation_params["Img2Img Upscale"] = self.scale

        self.width, self.height = self.get_final_size()

        image_mask = self.image_mask
        image_mask = self.image_mask


        if image_mask is not None:
        if image_mask is not None:
+65 −2
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@ import warnings
import gradio as gr
import gradio as gr
import gradio.routes
import gradio.routes
import gradio.utils
import gradio.utils
from gradio.events import Releaseable
import numpy as np
import numpy as np
from PIL import Image, PngImagePlugin
from PIL import Image, PngImagePlugin
from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call
from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call
@@ -138,6 +139,26 @@ def calc_resolution_hires(enable, width, height, hr_scale, hr_resize_x, hr_resiz
    return f"resize: from <span class='resolution'>{p.width}x{p.height}</span> to <span class='resolution'>{p.hr_resize_x or p.hr_upscale_to_x}x{p.hr_resize_y or p.hr_upscale_to_y}</span>"
    return f"resize: from <span class='resolution'>{p.width}x{p.height}</span> to <span class='resolution'>{p.hr_resize_x or p.hr_upscale_to_x}x{p.hr_resize_y or p.hr_upscale_to_y}</span>"




def calc_resolution_img2img(mode, scale, resize_x, resize_y, resize_mode, *i2i_images):
    init_img = None
    if mode in {0, 1, 3, 4}:
        init_img = i2i_images[mode]
    elif mode == 2:
        init_img = i2i_images[mode]["image"]

    if not init_img:
        return ""

    if scale > 1:
        width = int(init_img.width * scale)
        height = int(init_img.height * scale)
    else:
        width = resize_x
        height = resize_y

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


def apply_styles(prompt, prompt_neg, styles):
def apply_styles(prompt, prompt_neg, styles):
    prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles)
    prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles)
    prompt_neg = shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, styles)
    prompt_neg = shared.prompt_styles.apply_negative_styles_to_prompt(prompt_neg, styles)
@@ -755,6 +776,11 @@ def create_ui():
                    elif category == "dimensions":
                    elif category == "dimensions":
                        with FormRow():
                        with FormRow():
                            with gr.Column(elem_id="img2img_column_size", scale=4):
                            with gr.Column(elem_id="img2img_column_size", scale=4):
                                with FormRow(variant="compact"):
                                    final_resolution = FormHTML(value="", elem_id="img2img_finalres", label="Upscaled resolution", interactive=False)
                                with FormRow(variant="compact"):
                                    scale = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Upscale by", value=1.0, elem_id="img2img_scale")
                                with FormRow(variant="compact"):
                                    width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="img2img_width")
                                    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")
                                    height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="img2img_height")


@@ -824,6 +850,41 @@ def create_ui():
                                    outputs=[inpaint_controls, mask_alpha],
                                    outputs=[inpaint_controls, mask_alpha],
                                )
                                )


            img2img_resolution_preview_inputs = [dummy_component, # filled in by selected img2img tab index in _js
                                                 scale, width, height, resize_mode,
                                                 init_img, sketch, init_img_with_mask, inpaint_color_sketch, init_img_inpaint]
            for input in img2img_resolution_preview_inputs:
                if isinstance(input, Releaseable):
                    input.release(
                        fn=calc_resolution_img2img,
                        _js="get_img2img_tab_index_for_res_preview",
                        inputs=img2img_resolution_preview_inputs,
                        outputs=[final_resolution],
                        show_progress=False,
                    )
                    input.release(
                        None,
                        _js="onCalcResolutionImg2Img",
                        inputs=img2img_resolution_preview_inputs,
                        outputs=[],
                        show_progress=False,
                    )
                else:
                    input.change(
                        fn=calc_resolution_img2img,
                        _js="get_img2img_tab_index_for_res_preview",
                        inputs=img2img_resolution_preview_inputs,
                        outputs=[final_resolution],
                        show_progress=False,
                    )
                    input.change(
                        None,
                        _js="onCalcResolutionImg2Img",
                        inputs=img2img_resolution_preview_inputs,
                        outputs=[],
                        show_progress=False,
                    )

            img2img_gallery, generation_info, html_info, html_log = create_output_panel("img2img", opts.outdir_img2img_samples)
            img2img_gallery, generation_info, html_info, html_log = create_output_panel("img2img", opts.outdir_img2img_samples)


            connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
            connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
@@ -872,6 +933,7 @@ def create_ui():
                    subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox,
                    subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox,
                    height,
                    height,
                    width,
                    width,
                    scale,
                    resize_mode,
                    resize_mode,
                    inpaint_full_res,
                    inpaint_full_res,
                    inpaint_full_res_padding,
                    inpaint_full_res_padding,
@@ -957,6 +1019,7 @@ def create_ui():
                (seed, "Seed"),
                (seed, "Seed"),
                (width, "Size-1"),
                (width, "Size-1"),
                (height, "Size-2"),
                (height, "Size-2"),
                (scale, "Img2Img Upscale"),
                (batch_size, "Batch size"),
                (batch_size, "Batch size"),
                (subseed, "Variation seed"),
                (subseed, "Variation seed"),
                (subseed_strength, "Variation seed strength"),
                (subseed_strength, "Variation seed strength"),
Loading