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 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 12:27:00 +01:00
parent 14f84b879b
commit a385616fa2

View File

@@ -28,6 +28,9 @@ class JDL_PreviewToLoad:
"images": ("IMAGE",), "images": ("IMAGE",),
"filename": ("STRING", {"default": "preview"}), "filename": ("STRING", {"default": "preview"}),
}, },
"optional": {
"mask": ("MASK",),
},
"hidden": { "hidden": {
"prompt": "PROMPT", "prompt": "PROMPT",
"extra_pnginfo": "EXTRA_PNGINFO", "extra_pnginfo": "EXTRA_PNGINFO",
@@ -39,7 +42,7 @@ class JDL_PreviewToLoad:
OUTPUT_NODE = True OUTPUT_NODE = True
CATEGORY = "utils/image" 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) # Save to temp/ for preview (same as PreviewImage)
filename_prefix = "ComfyUI" + self.prefix_append filename_prefix = "ComfyUI" + self.prefix_append
full_output_folder, fname, counter, subfolder, filename_prefix = ( full_output_folder, fname, counter, subfolder, filename_prefix = (
@@ -85,6 +88,17 @@ class JDL_PreviewToLoad:
first_image = 255.0 * images[0].cpu().numpy() first_image = 255.0 * images[0].cpu().numpy()
first_img = Image.fromarray(np.clip(first_image, 0, 255).astype(np.uint8)) 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 metadata = None
if not args.disable_metadata: if not args.disable_metadata:
metadata = PngInfo() metadata = PngInfo()