The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Add Pad Image node with mirror padding and pad to multiple (#17224)
DoronGenzelHassf5c611e3849f
diff --git a/comfy_extras/nodes_images.py b/comfy_extras/nodes_images.pyindex 95b9547d0..64b09805d 100644--- a/comfy_extras/nodes_images.py+++ b/comfy_extras/nodes_images.py@@ -18,12 +18,15 @@ import uuidimport zlibimport comfy.utilsfrom av.video.reformatter import ColorPrimaries, ColorRange, ColorTrc+from enum import Enumfrom fractions import Fraction+from typing import TypedDictfrom PIL.Image import Exiffrom server import PromptServerfrom comfy_api.latest import ComfyExtension, IO, UIfrom comfy_execution.preview_tonemap import linear_to_preview+from comfy_extras.color_util import hex_to_rgbfrom comfy.cli_args import argsfrom typing_extensions import override@@ -646,6 +649,147 @@ class ResizeAndPadImage(IO.ComfyNode):resize_and_pad = execute # TODO: remove+class PadType(str, Enum):+ PAD_DIMENSIONS = "pad dimensions"+ PAD_BY = "pad by multiplier"+ PAD_TO_MULTIPLE = "pad to multiple"+++class PadStyle(str, Enum):+ COLOR = "color"+ MIRROR = "mirror image"+++def split_padding(total: int) -> tuple[int, int]:+ return total // 2, total - total // 2+++def mirror_indices(size: int, before: int, after: int, device: torch.device) -> torch.Tensor:+ # reflect without repeating the edge pixel, repeating the mirrored image when the padding exceeds the size+ period = max(2 * (size - 1), 1)+ idx = torch.arange(-before, size + after, device=device).abs() % period+ return torch.where(idx < size, idx, period - idx)+++def pad_mirror(image: torch.Tensor, left: int, top: int, right: int, bottom: int) -> torch.Tensor:+ _, height, width, _ = image.shape+ rows = mirror_indices(height, top, bottom, image.device)+ cols = mirror_indices(width, left, right, image.device)+ return image[:, rows[:, None], cols[None, :]]+++def feather_weight(size: int, before: int, after: int, feathering: int, device: torch.device) -> torch.Tensor:+ # integer distances: fp16/bf16 can't represent large pixel indices exactly+ i = torch.arange(size, device=device)+ dist = torch.full_like(i, feathering)+ if before > 0:+ dist = torch.minimum(dist, i)+ if after > 0:+ dist = torch.minimum(dist, size - 1 - i)+ return ((feathering - dist) / (feathering + 1)).square()+++def pad_color(image: torch.Tensor, left: int, top: int, right: int, bottom: int, color: str, feathering: int) -> torch.Tensor:+ batch, height, width, channels = image.shape+ rgb = [c / 255.0 for c in hex_to_rgb(color[:7])]+ fill = torch.tensor((rgb + [1.0] * channels)[:channels], dtype=image.dtype, device=image.device)++ out = torch.empty((batch, height + top + bottom, width + left + right, channels), dtype=image.dtype, device=image.device)+ out[:] = fill+ inner = out[:, top:top + height, left:left + width]+ inner.copy_(image)++ if feathering > 0:+ wy = feather_weight(height, top, bottom, feathering, image.device).to(image.dtype)+ wx = feather_weight(width, left, right, feathering, image.device).to(image.dtype)+ inner.lerp_(fill, torch.maximum(wy[:, None], wx[None, :]).unsqueeze(-1))+ return out+++class PadImageNode(IO.ComfyNode):+ class PadTypedDict(TypedDict):+ padding_type: PadType+ left: int+ top: int+ right: int+ bottom: int+ multiplier: float+ multiple: int++ class PadStyleTypedDict(TypedDict):+ padding_style: PadStyle+ color: str+ feathering: int++ @classmethod+ def define_schema(cls):+ return IO.Schema(+ node_id="PadImageNode",+ display_name="Pad Image",+ description="Pad an image with a solid color or a mirrored copy of its edges.",+ category="image/transform",+ search_aliases=["pad", "pad image", "padding", "reflect", "mirror", "extend canvas", "expand image", "border"],+ inputs=[+ IO.Image.Input("image"),+ IO.DynamicCombo.Input(+ "padding_type",+ tooltip="Select how much padding to add: exact amounts per side, a size multiplier, or up to the next multiple.",+ options=[+ IO.DynamicCombo.Option(PadType.PAD_DIMENSIONS, [+ IO.Int.Input("left", default=0, min=0, max=MAX_RESOLUTION, step=1, tooltip="Pixels to add on the left."),+ IO.Int.Input("top", default=0, min=0, max=MAX_RESOLUTION, step=1, tooltip="Pixels to add on the top."),+ IO.Int.Input("right", default=0, min=0, max=MAX_RESOLUTION, step=1, tooltip="Pixels to add on the right."),+ IO.Int.Input("bottom", default=0, min=0, max=MAX_RESOLUTION, step=1, tooltip="Pixels to add on the bottom."),+ ]),+ IO.DynamicCombo.Option(PadType.PAD_BY, [+ IO.Float.Input("multiplier", default=1.0, min=1.0, max=8.0, step=0.01, tooltip="Output size relative to the input (e.g., 1.5 adds 25% on each side). Padding is split evenly between opposite sides."),+ ]),+ IO.DynamicCombo.Option(PadType.PAD_TO_MULTIPLE, [+ IO.Int.Input("multiple", default=32, min=1, max=MAX_RESOLUTION, step=1, tooltip="Pad so width and height are divisible by this number. Padding is split evenly between opposite sides, with the extra pixel going to the right/bottom."),+ ]),+ ],+ ),+ IO.DynamicCombo.Input(+ "padding_style",+ tooltip="What to fill the padded area with.",+ options=[+ IO.DynamicCombo.Option(PadStyle.COLOR, [+ IO.Color.Input("color", default="#000000", tooltip="Fill color for the padded area. Alpha is ignored."),+ IO.Int.Input("feathering", default=0, min=0, max=MAX_RESOLUTION, step=1, tooltip="Blend this many pixels of the image edge into the fill color on the padded sides."),+ ]),+ IO.DynamicCombo.Option(PadStyle.MIRROR, []),+ ],+ ),+ ],+ outputs=[IO.Image.Output(display_name="image")],+ )++ @classmethod+ def execute(cls, image: IO.Image.Type, padding_type: PadTypedDict, padding_style: PadStyleTypedDict) -> IO.NodeOutput:+ _, height, width, _ = image.shape+ selected_type = padding_type["padding_type"]+ if selected_type == PadType.PAD_DIMENSIONS:+ left, top, right, bottom = padding_type["left"], padding_type["top"], padding_type["right"], padding_type["bottom"]+ elif selected_type == PadType.PAD_BY:+ left, right = split_padding(round(width * padding_type["multiplier"]) - width)+ top, bottom = split_padding(round(height * padding_type["multiplier"]) - height)+ elif selected_type == PadType.PAD_TO_MULTIPLE:+ multiple = padding_type["multiple"]+ left, right = split_padding(-width % multiple)+ top, bottom = split_padding(-height % multiple)+ else:+ raise ValueError(f"Unsupported padding type: {selected_type}")+ if left == top == right == bottom == 0:+ return IO.NodeOutput(image)++ selected_style = padding_style["padding_style"]+ if selected_style == PadStyle.MIRROR:+ return IO.NodeOutput(pad_mirror(image, left, top, right, bottom))+ elif selected_style == PadStyle.COLOR:+ return IO.NodeOutput(pad_color(image, left, top, right, bottom, padding_style["color"], padding_style["feathering"]))+ raise ValueError(f"Unsupported padding style: {selected_style}")++class SaveSVGNode(IO.ComfyNode):@classmethoddef define_schema(cls):@@ -2127,6 +2271,7 @@ class ImagesExtension(ComfyExtension):SaveSVGNode,ImageStitch,ResizeAndPadImage,+ PadImageNode,GetImageSize,GetLatentSize,ImageRotate,diff --git a/tests-unit/comfy_extras_test/pad_image_test.py b/tests-unit/comfy_extras_test/pad_image_test.pynew file mode 100644index 000000000..b32586b68--- /dev/null+++ b/tests-unit/comfy_extras_test/pad_image_test.py@@ -0,0 +1,124 @@+import torch+import torch.nn.functional as F++from comfy.cli_args import args as cli_args++if not torch.cuda.is_available():+ cli_args.cpu = True++from comfy_extras.nodes_images import PadImageNode, PadStyle, PadType # noqa: E402++MIRROR = {"padding_style": PadStyle.MIRROR}+++def dims(left=0, top=0, right=0, bottom=0):+ return {"padding_type": PadType.PAD_DIMENSIONS, "left": left, "top": top, "right": right, "bottom": bottom}+++def color(value="#000000", feathering=0):+ return {"padding_style": PadStyle.COLOR, "color": value, "feathering": feathering}+++def run(image, padding_type, padding_style):+ return PadImageNode.execute(image, padding_type, padding_style).result[0]+++def test_mirror_matches_torch_reflect():+ image = torch.rand(2, 7, 9, 3)++ out = run(image, dims(3, 2, 4, 5), MIRROR)++ expected = F.pad(image.movedim(-1, 1), (3, 4, 2, 5), mode="reflect").movedim(1, -1)+ assert torch.equal(out, expected)+++def test_mirror_padding_larger_than_image():+ image = torch.arange(4.0).reshape(1, 1, 4, 1)++ out = run(image, dims(left=7, right=16), MIRROR)++ assert out[0, 0, :, 0].tolist() == [1, 0, 1, 2, 3, 2, 1] + [0, 1, 2, 3, 2, 1] * 3 + [0, 1]+++def test_mirror_single_pixel_dimension():+ image = torch.rand(1, 1, 5, 3)++ out = run(image, dims(top=2, bottom=3), MIRROR)++ assert out.shape == (1, 6, 5, 3)+ assert torch.equal(out, image.expand(1, 6, 5, 3))+++def test_hdr_values_and_dtype_preserved():+ image = torch.rand(1, 8, 8, 3, dtype=torch.float16) * 20.0 - 2.0++ out = run(image, dims(4, 4, 4, 4), MIRROR)++ assert out.dtype == torch.float16+ assert torch.equal(out[:, 4:12, 4:12], image)+ assert out.max() == image.max() and out.min() == image.min()+++def test_rgba_mirror_keeps_alpha_and_color_pads_opaque():+ image = torch.rand(1, 6, 6, 4)++ mirrored = run(image, dims(2, 2, 2, 2), MIRROR)+ colored = run(image, dims(2, 2, 2, 2), color("#ff0000"))++ assert torch.equal(mirrored[:, 2:8, 0, 3], image[:, :, 2, 3])+ assert colored[0, 0, 0].tolist() == [1.0, 0.0, 0.0, 1.0]+++def test_color_with_alpha_uses_rgb():+ image = torch.rand(1, 4, 4, 3)++ out = run(image, dims(left=1), color("#00ff0080"))++ assert out[0, 0, 0].tolist() == [0.0, 1.0, 0.0]+++def test_pad_to_multiple():+ image = torch.rand(1, 33, 64, 3)++ out = run(image, {"padding_type": PadType.PAD_TO_MULTIPLE, "multiple": 32}, MIRROR)++ assert out.shape == (1, 64, 64, 3)+ assert torch.equal(out[:, 15:48], image)+++def test_pad_by_multiplier():+ image = torch.rand(1, 10, 20, 3)++ out = run(image, {"padding_type": PadType.PAD_BY, "multiplier": 1.5}, color("#ffffff"))++ assert out.shape == (1, 15, 30, 3)+ assert torch.equal(out[:, 2:12, 5:25], image)+ assert torch.all(out[:, :2] == 1.0) and torch.all(out[:, :, :5] == 1.0)+++def test_feathering_only_blends_padded_sides():+ image = torch.ones(1, 10, 10, 3)++ out = run(image, dims(left=4), color(feathering=4))+ region = out[0, :, 4:, 0]++ assert torch.all(out[0, :, :4] == 0.0)+ # weights (4/5)^2, (3/5)^2, (2/5)^2, (1/5)^2: the edge pixel is blended, not replaced+ assert torch.allclose(region[:, :4], torch.tensor([0.36, 0.64, 0.84, 0.96]).expand(10, 4))+ assert torch.all(region[:, 4:] == 1.0)+++def test_feathering_is_smooth_for_large_fp16_images():+ """fp16 can't represent pixel indices above 2048 exactly; the bottom/right feather must still match fp32."""+ height = 3000+ reference = run(torch.zeros(1, height, 4, 3), dims(bottom=4), color("#ffffff", feathering=16))+ half = run(torch.zeros(1, height, 4, 3, dtype=torch.float16), dims(bottom=4), color("#ffffff", feathering=16))++ assert torch.allclose(half[0, height - 16:height, 0, 0].float(), reference[0, height - 16:height, 0, 0], atol=1e-3)+++def test_no_padding_returns_input():+ image = torch.rand(1, 32, 32, 3)++ assert run(image, {"padding_type": PadType.PAD_TO_MULTIPLE, "multiple": 32}, MIRROR) is image+ assert run(image, dims(), color(feathering=8)) is image