Skip to content

Commit 008af2d

Browse files
xaedescodedealer
andauthored
Perform masked image restoration for GFPGAN, RealESRGAN, fixing #947
* Perform masked image restoration when using GFPGAN or RealESRGAN, fixing #947. Also fixes bug in image display when using masked image restoration with RealESRGAN. When the image is upscaled using RealESRGAN the image restoration can not use the original image because it has wrong resolution. In this case the image restoration will restore the non-regenerated parts of the image with an RealESRGAN upscaled version of the original input image. Modifications from GFPGAN or color correction in (un)masked parts are also restored to the original image by mask blending. * Update scripts/webui.py Co-authored-by: Thomas Mello <[email protected]>
1 parent f39b71a commit 008af2d

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

scripts/webui.py

+45-20
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,29 @@ def classToArrays( items, seed, n_iter ):
784784

785785
return all_seeds, n_iter, prompt_matrix_parts, all_prompts, needrows
786786

787+
def perform_masked_image_restoration(image, init_img, init_mask, mask_blur_strength, mask_restore, use_RealESRGAN, RealESRGAN):
788+
if not mask_restore:
789+
return image
790+
else:
791+
init_mask = init_mask.filter(ImageFilter.GaussianBlur(mask_blur_strength))
792+
init_mask = init_mask.convert('L')
793+
init_img = init_img.convert('RGB')
794+
image = image.convert('RGB')
795+
796+
if use_RealESRGAN and RealESRGAN is not None:
797+
output, img_mode = RealESRGAN.enhance(np.array(init_mask, dtype=np.uint8))
798+
init_mask = Image.fromarray(output)
799+
init_mask = init_mask.convert('L')
800+
801+
output, img_mode = RealESRGAN.enhance(np.array(init_img, dtype=np.uint8))
802+
init_img = Image.fromarray(output)
803+
init_img = init_img.convert('RGB')
804+
805+
image = Image.composite(init_img, image, init_mask)
806+
807+
return image
808+
809+
787810
def perform_color_correction(img_rgb, correction_target_lab, do_color_correction):
788811
try:
789812
from skimage import exposure
@@ -1022,6 +1045,11 @@ def process_images(
10221045
gfpgan_sample = restored_img[:,:,::-1]
10231046
gfpgan_image = Image.fromarray(gfpgan_sample)
10241047
gfpgan_image = perform_color_correction(gfpgan_image, correction_target, do_color_correction)
1048+
gfpgan_image = perform_masked_image_restoration(
1049+
gfpgan_image, init_img, init_mask,
1050+
mask_blur_strength, mask_restore,
1051+
use_RealESRGAN = False, RealESRGAN = None
1052+
)
10251053
gfpgan_metadata = copy.copy(metadata)
10261054
gfpgan_metadata.GFPGAN = True
10271055
ImageMetadata.set_on_image( gfpgan_image, gfpgan_metadata )
@@ -1040,6 +1068,11 @@ def process_images(
10401068
esrgan_sample = output[:,:,::-1]
10411069
esrgan_image = Image.fromarray(esrgan_sample)
10421070
esrgan_image = perform_color_correction(esrgan_image, correction_target, do_color_correction)
1071+
esrgan_image = perform_masked_image_restoration(
1072+
esrgan_image, init_img, init_mask,
1073+
mask_blur_strength, mask_restore,
1074+
use_RealESRGAN, RealESRGAN
1075+
)
10431076
ImageMetadata.set_on_image( esrgan_image, metadata )
10441077
save_sample(esrgan_image, sample_path_i, esrgan_filename, jpg_sample, write_info_files, write_sample_info_to_log_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
10451078
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, skip_metadata=False)
@@ -1057,6 +1090,11 @@ def process_images(
10571090
gfpgan_esrgan_sample = output[:,:,::-1]
10581091
gfpgan_esrgan_image = Image.fromarray(gfpgan_esrgan_sample)
10591092
gfpgan_esrgan_image = perform_color_correction(gfpgan_esrgan_image, correction_target, do_color_correction)
1093+
gfpgan_esrgan_image = perform_masked_image_restoration(
1094+
gfpgan_esrgan_image, init_img, init_mask,
1095+
mask_blur_strength, mask_restore,
1096+
use_RealESRGAN, RealESRGAN
1097+
)
10601098
ImageMetadata.set_on_image(gfpgan_esrgan_image, metadata)
10611099
save_sample(gfpgan_esrgan_image, sample_path_i, gfpgan_esrgan_filename, jpg_sample, write_info_files, write_sample_info_to_log_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback,
10621100
skip_save, skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, skip_metadata=False)
@@ -1068,26 +1106,13 @@ def process_images(
10681106
if imgProcessorTask == True:
10691107
output_images.append(image)
10701108

1071-
if mask_restore and init_mask:
1072-
#init_mask = init_mask if keep_mask else ImageOps.invert(init_mask)
1073-
init_mask = init_mask.filter(ImageFilter.GaussianBlur(mask_blur_strength))
1074-
init_mask = init_mask.convert('L')
1075-
init_img = init_img.convert('RGB')
1076-
image = image.convert('RGB')
1077-
1078-
if use_RealESRGAN and RealESRGAN is not None:
1079-
if RealESRGAN.model.name != realesrgan_model_name:
1080-
try_loading_RealESRGAN(realesrgan_model_name)
1081-
output, img_mode = RealESRGAN.enhance(np.array(init_img, dtype=np.uint8))
1082-
init_img = Image.fromarray(output)
1083-
init_img = init_img.convert('RGB')
1084-
1085-
output, img_mode = RealESRGAN.enhance(np.array(init_mask, dtype=np.uint8))
1086-
init_mask = Image.fromarray(output)
1087-
init_mask = init_mask.convert('L')
1088-
1089-
image = Image.composite(init_img, image, init_mask)
1090-
1109+
image = perform_masked_image_restoration(
1110+
image, init_img, init_mask,
1111+
mask_blur_strength, mask_restore,
1112+
# RealESRGAN image already processed in if-case above.
1113+
use_RealESRGAN = False, RealESRGAN = None
1114+
)
1115+
10911116
if not skip_save:
10921117
save_sample(image, sample_path_i, filename, jpg_sample, write_info_files, write_sample_info_to_log_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
10931118
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, False)

0 commit comments

Comments
 (0)