From a385616fa2e6efc7f77165bb701867e3db6ea1de Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Tue, 24 Feb 2026 12:27:00 +0100 Subject: [PATCH] Add optional mask input to PreviewToLoad node When a MASK is connected, it gets embedded as the alpha channel of the saved PNG. LoadImage then automatically extracts it as its mask output. Co-Authored-By: Claude Opus 4.6 --- image_preview.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/image_preview.py b/image_preview.py index fb4c36b..b72af0f 100644 --- a/image_preview.py +++ b/image_preview.py @@ -28,6 +28,9 @@ class JDL_PreviewToLoad: "images": ("IMAGE",), "filename": ("STRING", {"default": "preview"}), }, + "optional": { + "mask": ("MASK",), + }, "hidden": { "prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO", @@ -39,7 +42,7 @@ class JDL_PreviewToLoad: OUTPUT_NODE = True CATEGORY = "utils/image" - def preview_and_save(self, images, filename="preview", prompt=None, extra_pnginfo=None): + def preview_and_save(self, images, filename="preview", mask=None, prompt=None, extra_pnginfo=None): # Save to temp/ for preview (same as PreviewImage) filename_prefix = "ComfyUI" + self.prefix_append full_output_folder, fname, counter, subfolder, filename_prefix = ( @@ -85,6 +88,17 @@ class JDL_PreviewToLoad: first_image = 255.0 * images[0].cpu().numpy() first_img = Image.fromarray(np.clip(first_image, 0, 255).astype(np.uint8)) + # Embed mask as alpha channel so LoadImage extracts it + if mask is not None: + mask_data = mask[0].cpu().numpy() if mask.dim() == 3 else mask.cpu().numpy() + # LoadImage inverts alpha (mask = 1 - alpha), so save alpha = 1 - mask + alpha = np.clip((1.0 - mask_data) * 255.0, 0, 255).astype(np.uint8) + alpha_img = Image.fromarray(alpha) + if alpha_img.size != first_img.size: + alpha_img = alpha_img.resize(first_img.size, Image.LANCZOS) + first_img = first_img.convert("RGBA") + first_img.putalpha(alpha_img) + metadata = None if not args.disable_metadata: metadata = PngInfo()