From 1df031d82bb2adf551e8f7b083184ce6fd3f2c87 Mon Sep 17 00:00:00 2001 From: shawnington <88048838+shawnington@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:46:04 -0400 Subject: [PATCH] resolved issue with black and white images in nodes_mask.py Nodes that use the composite function accept both bw and rgb images. Both formats correctly display in the image preview node, however, the formats have transposed HW and incompatible C dimensions, even though they are both accepted as inputs. This change introduces a function that converts black and white inputs to rgb so that composite operations do not unexpectedly fail while the image preview displays an image successfully. --- comfy_extras/nodes_mask.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/comfy_extras/nodes_mask.py b/comfy_extras/nodes_mask.py index 29589b4ab..6c803626e 100644 --- a/comfy_extras/nodes_mask.py +++ b/comfy_extras/nodes_mask.py @@ -5,7 +5,19 @@ import comfy.utils from nodes import MAX_RESOLUTION +def tensor_to_rgb(img): + # convert from bw to rgb : cwh -> bchw + if img.shape[1] != 3: + if len(img.shape) == 3: + img = img.unsqueeze(0) + img = img.permute(0, 1, 3, 2).repeat(1, 3, 1, 1) + + return img + def composite(destination, source, x, y, mask = None, multiplier = 8, resize_source = False): + destination = tensor_to_rgb(destination) + source = tensor_to_rgb(source) + source = source.to(destination.device) if resize_source: source = torch.nn.functional.interpolate(source, size=(destination.shape[2], destination.shape[3]), mode="bilinear")