From 3551c8c361746f8d48bcdd45aa39c3db19d9939a Mon Sep 17 00:00:00 2001 From: Luis Quesada Date: Fri, 16 Jan 2026 19:51:21 +0100 Subject: [PATCH] Fix output_padding ignored if output_resize_to_target_size was not set --- inpaint_cropandstitch.py | 80 +++++++++++++++++++++++++++++++++++++++- pyproject.toml | 2 +- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/inpaint_cropandstitch.py b/inpaint_cropandstitch.py index 2c67125..d6fb630 100644 --- a/inpaint_cropandstitch.py +++ b/inpaint_cropandstitch.py @@ -445,6 +445,44 @@ class CPUProcessorLogic(ProcessorLogic): overflow = (new_h - image_h) // 2 new_y = -overflow + # Step 3b: When not resizing output, ensure dimensions are at least target dimensions + # This ensures output_padding works correctly even without resize (Option A: expand context, keep centered) + if not resize_output: + if new_w < target_w: + grow_w = target_w - new_w + new_x -= grow_w // 2 + new_w = target_w + # Recalculate bounds + if new_x < 0: + shift = -new_x + if new_x + new_w + shift <= image_w: + new_x += shift + else: + new_x = -((new_w - image_w) // 2) + elif new_x + new_w > image_w: + overflow = new_x + new_w - image_w + if new_x - overflow >= 0: + new_x -= overflow + else: + new_x = -((new_w - image_w) // 2) + if new_h < target_h: + grow_h = target_h - new_h + new_y -= grow_h // 2 + new_h = target_h + # Recalculate bounds + if new_y < 0: + shift = -new_y + if new_y + new_h + shift <= image_h: + new_y += shift + else: + new_y = -((new_h - image_h) // 2) + elif new_y + new_h > image_h: + overflow = new_y + new_h - image_h + if new_y - overflow >= 0: + new_y -= overflow + else: + new_y = -((new_h - image_h) // 2) + # Step 4: Grow the image to accommodate the new context area up_padding, down_padding, left_padding, right_padding = 0, 0, 0, 0 @@ -969,6 +1007,44 @@ class GPUProcessorLogic(ProcessorLogic): overflow = (new_h - image_h) // 2 new_y = -overflow + # Step 3b: When not resizing output, ensure dimensions are at least target dimensions + # This ensures output_padding works correctly even without resize (Option A: expand context, keep centered) + if not resize_output: + if new_w < target_w: + grow_w = target_w - new_w + new_x -= grow_w // 2 + new_w = target_w + # Recalculate bounds + if new_x < 0: + shift = -new_x + if new_x + new_w + shift <= image_w: + new_x += shift + else: + new_x = -((new_w - image_w) // 2) + elif new_x + new_w > image_w: + overflow = new_x + new_w - image_w + if new_x - overflow >= 0: + new_x -= overflow + else: + new_x = -((new_w - image_w) // 2) + if new_h < target_h: + grow_h = target_h - new_h + new_y -= grow_h // 2 + new_h = target_h + # Recalculate bounds + if new_y < 0: + shift = -new_y + if new_y + new_h + shift <= image_h: + new_y += shift + else: + new_y = -((new_h - image_h) // 2) + elif new_y + new_h > image_h: + overflow = new_y + new_h - image_h + if new_y - overflow >= 0: + new_y -= overflow + else: + new_y = -((new_h - image_h) // 2) + # Step 4: Grow the image to accommodate the new context area up_padding, down_padding, left_padding, right_padding = 0, 0, 0, 0 @@ -1003,9 +1079,9 @@ class GPUProcessorLogic(ProcessorLogic): # Fill the new extended areas with the edge values of the image if up_padding > 0: - expanded_image[:, :, :up_padding, left_padding:left_padding + image_w] = image[:, :, 0:1, left_padding:left_padding + image_w].repeat(1, 1, up_padding, 1) + expanded_image[:, :, :up_padding, left_padding:left_padding + image_w] = expanded_image[:, :, up_padding:up_padding + 1, left_padding:left_padding + image_w].repeat(1, 1, up_padding, 1) if down_padding > 0: - expanded_image[:, :, -down_padding:, left_padding:left_padding + image_w] = image[:, :, -1:, left_padding:left_padding + image_w].repeat(1, 1, down_padding, 1) + expanded_image[:, :, -down_padding:, left_padding:left_padding + image_w] = expanded_image[:, :, up_padding + image_h - 1:up_padding + image_h, left_padding:left_padding + image_w].repeat(1, 1, down_padding, 1) if left_padding > 0: expanded_image[:, :, up_padding:up_padding + image_h, :left_padding] = expanded_image[:, :, up_padding:up_padding + image_h, left_padding:left_padding+1].repeat(1, 1, 1, left_padding) if right_padding > 0: diff --git a/pyproject.toml b/pyproject.toml index 5736273..f2dc72d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-inpaint-cropandstitch" description = "The '✂️ Inpaint Crop' and '✂️ Inpaint Stitch' nodes enable inpainting only on masked area very easily: crop the image around the masked area with the Crop node, then use any standard workflow for sampling, then connect the sampled image to the Stitch node, which will put it back in place in the original image. These nodes enable faster sampling of smaller areas and take care of downsampling and upsampling to fit specific model and resource needs." -version = "3.0.1" +version = "3.0.2" license = { file = "LICENSE" } [project.urls]