@@ -784,6 +784,29 @@ def classToArrays( items, seed, n_iter ):
784
784
785
785
return all_seeds , n_iter , prompt_matrix_parts , all_prompts , needrows
786
786
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
+
787
810
def perform_color_correction (img_rgb , correction_target_lab , do_color_correction ):
788
811
try :
789
812
from skimage import exposure
@@ -1022,6 +1045,11 @@ def process_images(
1022
1045
gfpgan_sample = restored_img [:,:,::- 1 ]
1023
1046
gfpgan_image = Image .fromarray (gfpgan_sample )
1024
1047
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
+ )
1025
1053
gfpgan_metadata = copy .copy (metadata )
1026
1054
gfpgan_metadata .GFPGAN = True
1027
1055
ImageMetadata .set_on_image ( gfpgan_image , gfpgan_metadata )
@@ -1040,6 +1068,11 @@ def process_images(
1040
1068
esrgan_sample = output [:,:,::- 1 ]
1041
1069
esrgan_image = Image .fromarray (esrgan_sample )
1042
1070
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
+ )
1043
1076
ImageMetadata .set_on_image ( esrgan_image , metadata )
1044
1077
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 ,
1045
1078
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(
1057
1090
gfpgan_esrgan_sample = output [:,:,::- 1 ]
1058
1091
gfpgan_esrgan_image = Image .fromarray (gfpgan_esrgan_sample )
1059
1092
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
+ )
1060
1098
ImageMetadata .set_on_image (gfpgan_esrgan_image , metadata )
1061
1099
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 ,
1062
1100
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(
1068
1106
if imgProcessorTask == True :
1069
1107
output_images .append (image )
1070
1108
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
+
1091
1116
if not skip_save :
1092
1117
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 ,
1093
1118
skip_grid , sort_samples , sampler_name , ddim_eta , n_iter , batch_size , i , denoising_strength , resize_mode , False )
0 commit comments