Skip to content

Commit 59bb1d3

Browse files
committed
blur mask with color-sketch + add paint transparency slider
1 parent c34542a commit 59bb1d3

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

modules/img2img.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import traceback
55

66
import numpy as np
7-
from PIL import Image, ImageOps, ImageChops
7+
from PIL import Image, ImageOps, ImageFilter, ImageEnhance
88

99
from modules import devices
1010
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
@@ -40,7 +40,7 @@ def process_batch(p, input_dir, output_dir, args):
4040

4141
img = Image.open(image)
4242
# Use the EXIF orientation of photos taken by smartphones.
43-
img = ImageOps.exif_transpose(img)
43+
img = ImageOps.exif_transpose(img)
4444
p.init_images = [img] * p.batch_size
4545

4646
proc = modules.scripts.scripts_img2img.run(p, *args)
@@ -59,7 +59,7 @@ def process_batch(p, input_dir, output_dir, args):
5959
processed_image.save(os.path.join(output_dir, filename))
6060

6161

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):
6363
is_inpaint = mode == 1
6464
is_batch = mode == 2
6565

@@ -68,15 +68,20 @@ def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, pro
6868
if mask_mode == 0:
6969
image = init_img_with_mask
7070
is_mask_sketch = isinstance(image, dict)
71-
if is_mask_sketch:
71+
is_mask_paint = not is_mask_sketch
72+
if is_mask_sketch:
7273
# Sketch: mask iff. not transparent
7374
image, mask = image["image"], image["mask"]
74-
mask = np.array(mask)[..., -1] > 0
75+
pred = np.array(mask)[..., -1] > 0
7576
else:
7677
# Color-sketch: mask iff. painted over
7778
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))
8085
image = image.convert("RGB")
8186
# Uploaded mask
8287
else:
@@ -89,7 +94,7 @@ def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, pro
8994

9095
# Use the EXIF orientation of photos taken by smartphones.
9196
if image is not None:
92-
image = ImageOps.exif_transpose(image)
97+
image = ImageOps.exif_transpose(image)
9398

9499
assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
95100

modules/ui.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ def update_orig(image, state):
854854
init_img_inpaint = gr.Image(label="Image for img2img", show_label=False, source="upload", interactive=True, type="pil", visible=False, elem_id="img_inpaint_base")
855855
init_mask_inpaint = gr.Image(label="Mask", source="upload", interactive=True, type="pil", visible=False, elem_id="img_inpaint_mask")
856856

857+
show_mask_alpha = cmd_opts.gradio_inpaint_tool == "color-sketch"
858+
mask_alpha = gr.Slider(label="Mask transparency", interactive=show_mask_alpha, visible=show_mask_alpha)
857859
mask_blur = gr.Slider(label='Mask blur', minimum=0, maximum=64, step=1, value=4)
858860

859861
with gr.Row():
@@ -948,6 +950,7 @@ def update_orig(image, state):
948950
steps,
949951
sampler_index,
950952
mask_blur,
953+
mask_alpha,
951954
inpainting_fill,
952955
restore_faces,
953956
tiling,

0 commit comments

Comments
 (0)