diff --git a/node_helpers.py b/node_helpers.py index cac4e88dd..7f431f91b 100644 --- a/node_helpers.py +++ b/node_helpers.py @@ -86,6 +86,7 @@ def image_alpha_fix(destination, source): if destination.shape[-1] < source.shape[-1]: source = source[...,:destination.shape[-1]] elif destination.shape[-1] > source.shape[-1]: - source = torch.nn.functional.pad(source, (0, 1)) - source[..., -1] = 1.0 + pad = destination.shape[-1] - source.shape[-1] + source = torch.nn.functional.pad(source, (0, pad)) + source[..., -pad:] = 1.0 return destination, source diff --git a/tests-unit/node_helpers_test.py b/tests-unit/node_helpers_test.py new file mode 100644 index 000000000..edd12e88b --- /dev/null +++ b/tests-unit/node_helpers_test.py @@ -0,0 +1,27 @@ +import torch + +import node_helpers + + +class TestImageAlphaFix: + def test_pads_rgb_to_rgba(self): + destination = torch.zeros(1, 2, 2, 4) + source = torch.full((1, 2, 2, 3), 0.5) + _, source = node_helpers.image_alpha_fix(destination, source) + assert source.shape[-1] == 4 + assert torch.all(source[..., :3] == 0.5) # original channels preserved + assert torch.all(source[..., 3] == 1.0) # added alpha + + def test_pads_more_than_one_channel(self): + # channel gap > 1 (grayscale source, RGBA destination) must still match up. + destination = torch.zeros(1, 2, 2, 4) + source = torch.zeros(1, 2, 2, 1) + _, source = node_helpers.image_alpha_fix(destination, source) + assert source.shape[-1] == 4 + assert torch.all(source[..., 1:] == 1.0) + + def test_truncates_when_source_has_more_channels(self): + destination = torch.zeros(1, 2, 2, 3) + source = torch.ones(1, 2, 2, 4) + _, source = node_helpers.image_alpha_fix(destination, source) + assert source.shape[-1] == 3