From 3ebf7452c32398ffed53d838cfdc35bf5257f0b8 Mon Sep 17 00:00:00 2001 From: "Guo Y.K" Date: Thu, 23 Mar 2023 23:33:35 +0800 Subject: [PATCH] nodes: add ImagePadForOutpaint --- nodes.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/nodes.py b/nodes.py index 0b8be765..1f495900 100644 --- a/nodes.py +++ b/nodes.py @@ -908,6 +908,45 @@ class ImageInvert: return (s,) +class ImagePadForOutpaint: + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "left": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 64}), + "top": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 64}), + "right": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 64}), + "bottom": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 64}), + } + } + + RETURN_TYPES = ("IMAGE", "MASK") + FUNCTION = "expand_image" + + CATEGORY = "image" + + def expand_image(self, image, left, top, right, bottom): + d1, d2, d3, d4 = image.size() + + new_image = torch.zeros( + (d1, d2 + top + bottom, d3 + left + right, d4), + dtype=torch.float32, + ) + new_image[:, top:top + d2, left:left + d3, :] = image + + mask = torch.ones( + (d2 + top + bottom, d3 + left + right), + dtype=torch.float32, + ) + mask[top:top + d2, left:left + d3] = torch.zeros( + (d2, d3), + dtype=torch.float32, + ) + return (new_image, mask) + + NODE_CLASS_MAPPINGS = { "KSampler": KSampler, "CheckpointLoader": CheckpointLoader, @@ -926,6 +965,7 @@ NODE_CLASS_MAPPINGS = { "LoadImageMask": LoadImageMask, "ImageScale": ImageScale, "ImageInvert": ImageInvert, + "ImagePadForOutpaint": ImagePadForOutpaint, "ConditioningCombine": ConditioningCombine, "ConditioningSetArea": ConditioningSetArea, "KSamplerAdvanced": KSamplerAdvanced,