Skip to content

Commit 7045c84

Browse files
committed
Also use alpha channel from img2img input image as mask
1 parent 296d012 commit 7045c84

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

modules/img2img.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import math
2-
from PIL import Image
2+
from PIL import Image, ImageOps, ImageChops
33

44
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
55
from modules.shared import opts, state
@@ -16,7 +16,9 @@ def img2img(prompt: str, init_img, init_img_with_mask, steps: int, sampler_index
1616

1717
if is_inpaint:
1818
image = init_img_with_mask['image']
19-
mask = init_img_with_mask['mask']
19+
alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')
20+
mask = ImageChops.lighter(alpha_mask, init_img_with_mask['mask'].convert('L')).convert('RGBA')
21+
image = image.convert('RGB')
2022
else:
2123
image = init_img
2224
mask = None

modules/ui.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
323323
with gr.Group():
324324
switch_mode = gr.Radio(label='Mode', elem_id="img2img_mode", choices=['Redraw whole image', 'Inpaint a part of image', 'Loopback', 'SD upscale'], value='Redraw whole image', type="index", show_label=False)
325325
init_img = gr.Image(label="Image for img2img", source="upload", interactive=True, type="pil")
326-
init_img_with_mask = gr.Image(label="Image for inpainting with mask", elem_id="img2maskimg", source="upload", interactive=True, type="pil", tool="sketch", visible=False)
326+
init_img_with_mask = gr.Image(label="Image for inpainting with mask", elem_id="img2maskimg", source="upload", interactive=True, type="pil", tool="sketch", visible=False, image_mode="RGBA")
327327
resize_mode = gr.Radio(label="Resize mode", show_label=False, choices=["Just resize", "Crop and resize", "Resize and fill"], type="index", value="Just resize")
328328

329329
steps = gr.Slider(minimum=1, maximum=150, step=1, label="Sampling Steps", value=20)

0 commit comments

Comments
 (0)