Skip to content

Commit

Permalink
Add a toggle to enable the original masking behaviour
Browse files Browse the repository at this point in the history
 - Re-applies the masked areas (or unmasked areas depending on the setting)
 - Addresses Sygil-Dev/stable-diffusion#153
  • Loading branch information
anon-hlhl committed Aug 31, 2022
1 parent fecac5c commit a38dfe2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def classToArrays( items, seed, n_iter ):
def process_images(
outpath, func_init, func_sample, prompt, seed, sampler_name, skip_grid, skip_save, batch_size,
n_iter, steps, cfg_scale, width, height, prompt_matrix, use_GFPGAN, use_RealESRGAN, realesrgan_model_name,
fp, ddim_eta=0.0, do_not_save_grid=False, normalize_prompt_weights=True, init_img=None, init_mask=None,
fp, ddim_eta=0.0, do_not_save_grid=False, normalize_prompt_weights=True, init_img=None, init_mask=None, init_mask_restore=False,
keep_mask=False, mask_blur_strength=3, denoising_strength=0.75, resize_mode=None, uses_loopback=False,
uses_random_seed_loopback=False, sort_samples=True, write_info_files=True, jpg_sample=False,
variant_amount=0.0, variant_seed=None):
Expand Down Expand Up @@ -842,6 +842,16 @@ def process_images(
x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
x_sample = x_sample.astype(np.uint8)
image = Image.fromarray(x_sample)

if init_mask is not None and init_img is not None and init_mask_restore:
# During inpainting, some fidelity of the original image may be lost,
# here the original image data is overlayed if restoration is selected ('Restore masked areas')
init_mask = init_mask.filter(ImageFilter.GaussianBlur(mask_blur_strength))
init_mask = init_mask.convert('L')
init_img = init_img.convert('RGB')
image = image.convert('RGB')
image = Image.composite(init_img, image, init_mask)

original_sample = x_sample
original_filename = filename
if use_GFPGAN and GFPGAN is not None and not use_RealESRGAN:
Expand Down Expand Up @@ -1095,8 +1105,9 @@ def img2img(prompt: str, image_editor_mode: str, init_info, mask_mode: str, mask
sort_samples = 6 in toggles
write_info_files = 7 in toggles
jpg_sample = 8 in toggles
use_GFPGAN = 9 in toggles
use_RealESRGAN = 10 in toggles
init_mask_restore = 9 in toggles
use_GFPGAN = 10 in toggles
use_RealESRGAN = 11 in toggles

if sampler_name == 'DDIM':
sampler = DDIMSampler(model)
Expand Down Expand Up @@ -1252,6 +1263,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
normalize_prompt_weights=normalize_prompt_weights,
init_img=init_img,
init_mask=init_mask,
init_mask_restore=init_mask_restore,
keep_mask=keep_mask,
mask_blur_strength=mask_blur_strength,
denoising_strength=denoising_strength,
Expand Down Expand Up @@ -1308,6 +1320,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
normalize_prompt_weights=normalize_prompt_weights,
init_img=init_img,
init_mask=init_mask,
init_mask_restore=init_mask_restore,
keep_mask=keep_mask,
mask_blur_strength=mask_blur_strength,
denoising_strength=denoising_strength,
Expand Down Expand Up @@ -1456,6 +1469,7 @@ def run_RealESRGAN(image, model_name: str):
'Sort samples by prompt',
'Write sample info files',
'jpg samples',
'Restore masked areas (recommended if using loopback)'
]
if GFPGAN is not None:
img2img_toggles.append('Fix faces using GFPGAN')
Expand Down

0 comments on commit a38dfe2

Please sign in to comment.