Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Don't adjust the alpha channel in the image color adjustment nodes (#15629)

Christian Byrne25dfc16f9ac0
diff --git a/comfy_extras/nodes_dataset.py b/comfy_extras/nodes_dataset.py
index 6ee0d91c3..361328bc9 100644
--- a/comfy_extras/nodes_dataset.py
+++ b/comfy_extras/nodes_dataset.py
@@ -1067,7 +1067,6 @@ class NormalizeImagesNode(ImageProcessingNode):
min=0.0,
max=1.0,
tooltip="Mean value for normalization.",
- advanced=True,
),
io.Float.Input(
"std",
@@ -1075,13 +1074,15 @@ class NormalizeImagesNode(ImageProcessingNode):
min=0.001,
max=1.0,
tooltip="Standard deviation for normalization.",
- advanced=True,
),
]
@classmethod
def _process(cls, image, mean, std):
- return (image - mean) / std
+ out = (image - mean) / std
+ if image.shape[-1] == 4: # alpha stores transparency, not color
+ out[..., 3] = image[..., 3]
+ return out
class AdjustBrightnessNode(ImageProcessingNode):
@@ -1103,7 +1104,10 @@ class AdjustBrightnessNode(ImageProcessingNode):
@classmethod
def _process(cls, image, factor):
- return (image * factor).clamp(0.0, 1.0)
+ out = (image * factor).clamp(0.0, 1.0)
+ if image.shape[-1] == 4: # alpha stores transparency, not color
+ out[..., 3] = image[..., 3]
+ return out
class AdjustContrastNode(ImageProcessingNode):
@@ -1125,7 +1129,10 @@ class AdjustContrastNode(ImageProcessingNode):
@classmethod
def _process(cls, image, factor):
- return ((image - 0.5) * factor + 0.5).clamp(0.0, 1.0)
+ out = ((image - 0.5) * factor + 0.5).clamp(0.0, 1.0)
+ if image.shape[-1] == 4: # alpha stores transparency, not color
+ out[..., 3] = image[..., 3]
+ return out
class ShuffleDatasetNode(ImageProcessingNode):
diff --git a/tests-unit/comfy_extras_test/dataset_color_nodes_alpha_test.py b/tests-unit/comfy_extras_test/dataset_color_nodes_alpha_test.py
new file mode 100644
index 000000000..70b63abd6
--- /dev/null
+++ b/tests-unit/comfy_extras_test/dataset_color_nodes_alpha_test.py
@@ -0,0 +1,78 @@
+import pytest
+import torch
+
+from comfy.cli_args import args as cli_args
+
+if not torch.cuda.is_available():
+ cli_args.cpu = True
+
+from comfy_extras.nodes_dataset import ( # noqa: E402
+ AdjustBrightnessNode,
+ AdjustContrastNode,
+ NormalizeImagesNode,
+)
+
+# (node, kwargs) for each colour adjustment that must leave alpha alone.
+CASES = [
+ (NormalizeImagesNode, {"mean": 0.5, "std": 0.5}),
+ (AdjustBrightnessNode, {"factor": 1.5}),
+ (AdjustContrastNode, {"factor": 1.5}),
+]
+IDS = [node.__name__ for node, _ in CASES]
+
+
+def image(channels, value=0.25, alpha=0.8, size=4):
+ # alpha 0.8 on purpose: 0.0, 0.5 and 1.0 are fixed points of one or more of
+ # these transforms, so they would hide the bug.
+ t = torch.full((1, size, size, channels), value)
+ if channels == 4:
+ t[..., 3] = alpha
+ return t
+
+
[email protected](("node", "kwargs"), CASES, ids=IDS)
+def test_rgb_is_adjusted_on_every_channel(node, kwargs):
+ src = image(3)
+
+ out = node._process(src, **kwargs)
+
+ assert out.shape == src.shape
+ assert not torch.equal(out, src)
+
+
[email protected](("node", "kwargs"), CASES, ids=IDS)
+def test_rgba_keeps_alpha_untouched(node, kwargs):
+ src = image(4)
+
+ out = node._process(src, **kwargs)
+
+ assert out.shape[-1] == 4
+ assert torch.equal(out[..., 3], src[..., 3])
+
+
[email protected](("node", "kwargs"), CASES, ids=IDS)
+def test_colour_channels_still_change(node, kwargs):
+ src = image(4)
+
+ out = node._process(src, **kwargs)
+
+ assert not torch.equal(out[..., :3], src[..., :3])
+
+
[email protected](("node", "kwargs"), CASES, ids=IDS)
+def test_transparent_pixels_stay_transparent(node, kwargs):
+ src = image(4, alpha=0.0)
+
+ out = node._process(src, **kwargs)
+
+ assert torch.all(out[..., 3] == 0.0)
+
+
[email protected](("node", "kwargs"), CASES, ids=IDS)
+def test_does_not_mutate_input(node, kwargs):
+ src = image(4)
+ before = src.clone()
+
+ node._process(src, **kwargs)
+
+ assert torch.equal(src, before)