[refactor] Rename 'border' to 'spacing' for semantic accuracy

- Change border_width/border_color to spacing_width/spacing_color in API
- Update all tests to use spacing terminology
- Update comments and variable names throughout
- More accurately describes the gap/separator between images
This commit is contained in:
bymyself 2025-06-01 01:18:18 -07:00
parent fcbb1a7ff7
commit 88acef8cdd
2 changed files with 50 additions and 49 deletions

View File

@ -241,11 +241,11 @@ class ImageStitch:
"image1": ("IMAGE",), "image1": ("IMAGE",),
"direction": (["right", "down", "left", "up"], {"default": "right"}), "direction": (["right", "down", "left", "up"], {"default": "right"}),
"match_image_size": ("BOOLEAN", {"default": True}), "match_image_size": ("BOOLEAN", {"default": True}),
"border_width": ( "spacing_width": (
"INT", "INT",
{"default": 8, "min": 0, "max": 1024, "step": 2}, {"default": 0, "min": 0, "max": 1024, "step": 2},
), ),
"border_color": ( "spacing_color": (
["white", "black", "red", "green", "blue"], ["white", "black", "red", "green", "blue"],
{"default": "white"}, {"default": "white"},
), ),
@ -261,6 +261,7 @@ class ImageStitch:
DESCRIPTION = """ DESCRIPTION = """
Stitches image2 to image1 in the specified direction. Stitches image2 to image1 in the specified direction.
If image2 is not provided, returns image1 unchanged. If image2 is not provided, returns image1 unchanged.
Optional spacing can be added between images.
""" """
def stitch( def stitch(
@ -268,8 +269,8 @@ If image2 is not provided, returns image1 unchanged.
image1, image1,
direction, direction,
match_image_size, match_image_size,
border_width, spacing_width,
border_color, spacing_color,
image2=None, image2=None,
): ):
if image2 is None: if image2 is None:
@ -360,9 +361,9 @@ If image2 is not provided, returns image1 unchanged.
dim=-1, dim=-1,
) )
# Add border if specified # Add spacing if specified
if border_width > 0: if spacing_width > 0:
border_width = border_width + (border_width % 2) # Ensure even spacing_width = spacing_width + (spacing_width % 2) # Ensure even
color_map = { color_map = {
"white": 1.0, "white": 1.0,
@ -371,39 +372,39 @@ If image2 is not provided, returns image1 unchanged.
"green": (0.0, 1.0, 0.0), "green": (0.0, 1.0, 0.0),
"blue": (0.0, 0.0, 1.0), "blue": (0.0, 0.0, 1.0),
} }
color_val = color_map[border_color] color_val = color_map[spacing_color]
if direction in ["left", "right"]: if direction in ["left", "right"]:
border_shape = ( spacing_shape = (
image1.shape[0], image1.shape[0],
max(image1.shape[1], image2.shape[1]), max(image1.shape[1], image2.shape[1]),
border_width, spacing_width,
image1.shape[-1], image1.shape[-1],
) )
else: else:
border_shape = ( spacing_shape = (
image1.shape[0], image1.shape[0],
border_width, spacing_width,
max(image1.shape[2], image2.shape[2]), max(image1.shape[2], image2.shape[2]),
image1.shape[-1], image1.shape[-1],
) )
border = torch.full(border_shape, 0.0, device=image1.device) spacing = torch.full(spacing_shape, 0.0, device=image1.device)
if isinstance(color_val, tuple): if isinstance(color_val, tuple):
for i, c in enumerate(color_val): for i, c in enumerate(color_val):
if i < border.shape[-1]: if i < spacing.shape[-1]:
border[..., i] = c spacing[..., i] = c
if border.shape[-1] == 4: # Add alpha if spacing.shape[-1] == 4: # Add alpha
border[..., 3] = 1.0 spacing[..., 3] = 1.0
else: else:
border[..., : min(3, border.shape[-1])] = color_val spacing[..., : min(3, spacing.shape[-1])] = color_val
if border.shape[-1] == 4: if spacing.shape[-1] == 4:
border[..., 3] = 1.0 spacing[..., 3] = 1.0
# Concatenate images # Concatenate images
images = [image2, image1] if direction in ["left", "up"] else [image1, image2] images = [image2, image1] if direction in ["left", "up"] else [image1, image2]
if border_width > 0: if spacing_width > 0:
images.insert(1, border) images.insert(1, spacing)
concat_dim = 2 if direction in ["left", "right"] else 1 concat_dim = 2 if direction in ["left", "right"] else 1
return (torch.cat(images, dim=concat_dim),) return (torch.cat(images, dim=concat_dim),)

View File

@ -111,54 +111,54 @@ class TestImageStitch:
# Both images should be padded to width 64 # Both images should be padded to width 64
assert result[0].shape == (1, 56, 64, 3) # 32 + 24 height, max(64,48) width assert result[0].shape == (1, 56, 64, 3) # 32 + 24 height, max(64,48) width
def test_border_horizontal(self): def test_spacing_horizontal(self):
"""Test border addition in horizontal concatenation""" """Test spacing addition in horizontal concatenation"""
node = ImageStitch() node = ImageStitch()
image1 = self.create_test_image(height=32, width=32) image1 = self.create_test_image(height=32, width=32)
image2 = self.create_test_image(height=32, width=24) image2 = self.create_test_image(height=32, width=24)
border_width = 16 spacing_width = 16
result = node.stitch(image1, "right", False, border_width, "white", image2) result = node.stitch(image1, "right", False, spacing_width, "white", image2)
# Expected width: 32 + 16 (border) + 24 = 72 # Expected width: 32 + 16 (spacing) + 24 = 72
assert result[0].shape == (1, 32, 72, 3) assert result[0].shape == (1, 32, 72, 3)
def test_border_vertical(self): def test_spacing_vertical(self):
"""Test border addition in vertical concatenation""" """Test spacing addition in vertical concatenation"""
node = ImageStitch() node = ImageStitch()
image1 = self.create_test_image(height=32, width=32) image1 = self.create_test_image(height=32, width=32)
image2 = self.create_test_image(height=24, width=32) image2 = self.create_test_image(height=24, width=32)
border_width = 16 spacing_width = 16
result = node.stitch(image1, "down", False, border_width, "white", image2) result = node.stitch(image1, "down", False, spacing_width, "white", image2)
# Expected height: 32 + 16 (border) + 24 = 72 # Expected height: 32 + 16 (spacing) + 24 = 72
assert result[0].shape == (1, 72, 32, 3) assert result[0].shape == (1, 72, 32, 3)
def test_border_color_values(self): def test_spacing_color_values(self):
"""Test that border colors are applied correctly""" """Test that spacing colors are applied correctly"""
node = ImageStitch() node = ImageStitch()
image1 = self.create_test_image(height=32, width=32) image1 = self.create_test_image(height=32, width=32)
image2 = self.create_test_image(height=32, width=32) image2 = self.create_test_image(height=32, width=32)
# Test white border # Test white spacing
result_white = node.stitch(image1, "right", False, 16, "white", image2) result_white = node.stitch(image1, "right", False, 16, "white", image2)
# Check that border region contains white values (close to 1.0) # Check that spacing region contains white values (close to 1.0)
border_region = result_white[0][:, :, 32:48, :] # Middle 16 pixels spacing_region = result_white[0][:, :, 32:48, :] # Middle 16 pixels
assert torch.all(border_region >= 0.9) # Should be close to white assert torch.all(spacing_region >= 0.9) # Should be close to white
# Test black border # Test black spacing
result_black = node.stitch(image1, "right", False, 16, "black", image2) result_black = node.stitch(image1, "right", False, 16, "black", image2)
border_region = result_black[0][:, :, 32:48, :] spacing_region = result_black[0][:, :, 32:48, :]
assert torch.all(border_region <= 0.1) # Should be close to black assert torch.all(spacing_region <= 0.1) # Should be close to black
def test_odd_border_width_made_even(self): def test_odd_spacing_width_made_even(self):
"""Test that odd border widths are made even""" """Test that odd spacing widths are made even"""
node = ImageStitch() node = ImageStitch()
image1 = self.create_test_image(height=32, width=32) image1 = self.create_test_image(height=32, width=32)
image2 = self.create_test_image(height=32, width=32) image2 = self.create_test_image(height=32, width=32)
# Use odd border width # Use odd spacing width
result = node.stitch(image1, "right", False, 15, "white", image2) result = node.stitch(image1, "right", False, 15, "white", image2)
# Should be made even (16), so total width = 32 + 16 + 32 = 80 # Should be made even (16), so total width = 32 + 16 + 32 = 80
@ -221,19 +221,19 @@ class TestImageStitch:
result = node.stitch(image1, direction, False, 0, "white", image2) result = node.stitch(image1, direction, False, 0, "white", image2)
assert result[0].shape == (1, 32, 64, 3) if direction in ["right", "left"] else (1, 64, 32, 3) assert result[0].shape == (1, 32, 64, 3) if direction in ["right", "left"] else (1, 64, 32, 3)
def test_batch_size_channel_border_integration(self): def test_batch_size_channel_spacing_integration(self):
"""Test integration of batch matching, channel matching, size matching, and borders""" """Test integration of batch matching, channel matching, size matching, and spacings"""
node = ImageStitch() node = ImageStitch()
image1 = self.create_test_image(batch_size=2, height=64, width=48, channels=3) image1 = self.create_test_image(batch_size=2, height=64, width=48, channels=3)
image2 = self.create_test_image(batch_size=1, height=32, width=32, channels=4) image2 = self.create_test_image(batch_size=1, height=32, width=32, channels=4)
result = node.stitch(image1, "right", True, 8, "red", image2) result = node.stitch(image1, "right", True, 8, "red", image2)
# Should handle: batch matching, size matching, channel matching, border # Should handle: batch matching, size matching, channel matching, spacing
assert result[0].shape[0] == 2 # Batch size matched assert result[0].shape[0] == 2 # Batch size matched
assert result[0].shape[-1] == 4 # Channels matched to max assert result[0].shape[-1] == 4 # Channels matched to max
assert result[0].shape[1] == 64 # Height from image1 (size matching) assert result[0].shape[1] == 64 # Height from image1 (size matching)
# Width should be: 48 + 8 (border) + resized_image2_width # Width should be: 48 + 8 (spacing) + resized_image2_width
expected_image2_width = int(64 * (32/32)) # Resized to height 64 expected_image2_width = int(64 * (32/32)) # Resized to height 64
expected_total_width = 48 + 8 + expected_image2_width expected_total_width = 48 + 8 + expected_image2_width
assert result[0].shape[2] == expected_total_width assert result[0].shape[2] == expected_total_width