4
4
import traceback
5
5
6
6
import numpy as np
7
- from PIL import Image , ImageOps , ImageChops
7
+ from PIL import Image , ImageOps , ImageFilter , ImageEnhance
8
8
9
9
from modules import devices
10
10
from modules .processing import Processed , StableDiffusionProcessingImg2Img , process_images
@@ -40,7 +40,7 @@ def process_batch(p, input_dir, output_dir, args):
40
40
41
41
img = Image .open (image )
42
42
# Use the EXIF orientation of photos taken by smartphones.
43
- img = ImageOps .exif_transpose (img )
43
+ img = ImageOps .exif_transpose (img )
44
44
p .init_images = [img ] * p .batch_size
45
45
46
46
proc = modules .scripts .scripts_img2img .run (p , * args )
@@ -59,7 +59,7 @@ def process_batch(p, input_dir, output_dir, args):
59
59
processed_image .save (os .path .join (output_dir , filename ))
60
60
61
61
62
- def img2img (mode : int , prompt : str , negative_prompt : str , prompt_style : str , prompt_style2 : str , init_img , init_img_with_mask , init_img_with_mask_orig , init_img_inpaint , init_mask_inpaint , mask_mode , steps : int , sampler_index : int , mask_blur : int , inpainting_fill : int , restore_faces : bool , tiling : bool , n_iter : int , batch_size : int , 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 , * args ):
62
+ def img2img (mode : int , prompt : str , negative_prompt : str , prompt_style : str , prompt_style2 : str , init_img , init_img_with_mask , init_img_with_mask_orig , init_img_inpaint , init_mask_inpaint , mask_mode , 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 , 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 , * args ):
63
63
is_inpaint = mode == 1
64
64
is_batch = mode == 2
65
65
@@ -68,15 +68,20 @@ def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, pro
68
68
if mask_mode == 0 :
69
69
image = init_img_with_mask
70
70
is_mask_sketch = isinstance (image , dict )
71
- if is_mask_sketch :
71
+ is_mask_paint = not is_mask_sketch
72
+ if is_mask_sketch :
72
73
# Sketch: mask iff. not transparent
73
74
image , mask = image ["image" ], image ["mask" ]
74
- mask = np .array (mask )[..., - 1 ] > 0
75
+ pred = np .array (mask )[..., - 1 ] > 0
75
76
else :
76
77
# Color-sketch: mask iff. painted over
77
78
orig = init_img_with_mask_orig or image
78
- mask = np .any (np .array (image ) != np .array (orig ), axis = - 1 )
79
- mask = Image .fromarray (mask .astype (np .uint8 ) * 255 , "L" )
79
+ pred = np .any (np .array (image ) != np .array (orig ), axis = - 1 )
80
+ mask = Image .fromarray (pred .astype (np .uint8 ) * 255 , "L" )
81
+ if is_mask_paint :
82
+ mask = ImageEnhance .Brightness (mask ).enhance (1 - mask_alpha / 100 )
83
+ blur = ImageFilter .GaussianBlur (mask_blur )
84
+ image = Image .composite (image .filter (blur ), orig , mask .filter (blur ))
80
85
image = image .convert ("RGB" )
81
86
# Uploaded mask
82
87
else :
@@ -89,7 +94,7 @@ def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, pro
89
94
90
95
# Use the EXIF orientation of photos taken by smartphones.
91
96
if image is not None :
92
- image = ImageOps .exif_transpose (image )
97
+ image = ImageOps .exif_transpose (image )
93
98
94
99
assert 0. <= denoising_strength <= 1. , 'can only work with strength in [0.0, 1.0]'
95
100
0 commit comments