Gourieff / sd-webui-reactor

Fast and Simple Face Swap Extension for StableDiffusion WebUI (A1111 SD WebUI, SD WebUI Forge, SD.Next, Cagliostro)
GNU Affero General Public License v3.0
2.17k stars 235 forks source link

[Feature]: Add a 'Mask' input image parameter #429

Open altoiddealer opened 4 weeks ago

altoiddealer commented 4 weeks ago

Feature description

Related to #292 which I'm guessing is perhaps too heavy handed or overly technical.

I'm suggesting to add an API param and UI input for a 'mask' image (B+W map).

In regards to the UI, it would only be practical with controlnet, or lightly denoised img2img, or Extras - so such an addition may only be applicable to the Img2Img and Extras tab.

Ideally, the UI would include a "Create mask canvas" thing like the newer versions of ControlNet have, where user can quickly paint their mask.

altoiddealer commented 3 weeks ago

Just following up that I've added this to my discord bot and it is working.

async def apply_reactor_mask(temp_dir, images, pnginfo, reactor_mask):
    try:
        reactor_mask = Image.open(io.BytesIO(base64.b64decode(reactor_mask))).convert('L')
        orig_image = images[0]                                          # Open original image
        face_image = images.pop(1)                                      # Open image with faceswap applied
        face_image.putalpha(reactor_mask)                               # Apply reactor mask as alpha to faceswap image
        orig_image.paste(face_image, (0, 0), face_image)                # Paste the masked faceswap image onto the original
        orig_image.save(f'{temp_dir}/temp_img_0.png', pnginfo=pnginfo)  # Save the image with correct pnginfo
        images[0] = orig_image                                          # Replace first image in images list
        return images
    except Exception as e:
        logging.error(f'Error masking ReActor output images: {e}')

In case that's half the work towards implementing such a feature...