From 5f1e25d5da4ef599766e5373a49d524c82e5d8cd Mon Sep 17 00:00:00 2001 From: Level Pixel Dev Date: Mon, 11 Aug 2025 01:29:08 +0600 Subject: [PATCH] Added additional parameter to CroppedAspectSizeParameters for Inpaint Crop --- nodes/image/inpaint_crop_stitch_LP.py | 32 +++++++++++++++++++++++---- pyproject.toml | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/nodes/image/inpaint_crop_stitch_LP.py b/nodes/image/inpaint_crop_stitch_LP.py index 9ca1389..8d123a6 100644 --- a/nodes/image/inpaint_crop_stitch_LP.py +++ b/nodes/image/inpaint_crop_stitch_LP.py @@ -850,6 +850,7 @@ class CroppedAspectSizeParameters: "mask": ("MASK", ), "target_size": ("INT", {"default": 1024, "min": 0, "max": nodes.MAX_RESOLUTION, "step": 1}), "aspect_ratio_limit": ("FLOAT", {"default": 2, "min": 1, "max": 100, "step": 0.01}), + "crop_size_mode": (["auto", "no larger than mask", "no smaller than mask"], {"default": "auto", "tooltip": "How to constrain the crop size relative to the mask+context area."}), }, "optional": { "optional_context_mask": ("MASK", ), @@ -860,14 +861,37 @@ class CroppedAspectSizeParameters: RETURN_NAMES = ("cropped_size_parameters",) FUNCTION = "cropped_aspect_size_parameters" CATEGORY = "LevelPixel/Inpaint" - def cropped_aspect_size_parameters(self, mask, target_size=1024, aspect_ratio_limit=2, optional_context_mask=None): + def cropped_aspect_size_parameters(self, mask, target_size=1024, aspect_ratio_limit=2, crop_size_mode="Auto", optional_context_mask=None): if optional_context_mask is not None: assert optional_context_mask.shape[1:] == mask.shape[1:3], f"optional_context_mask dimensions do not match mask dimensions. Expected {mask.shape[1:3]}, got {optional_context_mask.shape[1:]}" context_mask = optional_context_mask.squeeze(-1) - mask = ((mask > 0) | (context_mask > 0)).float() - cropped_size_parameters = calculate_target_size(mask, target_size, aspect_ratio_limit) - cropped_size_parameters["batch_mode"] = False + combined_mask = ((mask > 0) | (context_mask > 0)).float() + else: + combined_mask = mask + cropped_size_parameters = calculate_target_size(combined_mask, target_size, aspect_ratio_limit) + + if optional_context_mask is not None: + assert optional_context_mask.shape[1:] == mask.shape[1:3], f"optional_context_mask dimensions do not match mask dimensions. Expected {mask.shape[1:3]}, got {optional_context_mask.shape[1:]}" + new_combined_mask = ((mask > 0) | (optional_context_mask > 0)).float() + else: + new_combined_mask = mask + mask_np = new_combined_mask.cpu().numpy().squeeze() + mask_pil = Image.fromarray(np.clip(255. * mask_np, 0, 255).astype(np.uint8)) + + region_mask, crop_data = crop_region(mask_pil, square=False) + + (width, height), (left, top, right, bottom) = crop_data + + if crop_size_mode == "no larger than mask": + if cropped_size_parameters["target_width"] > width and cropped_size_parameters["target_height"] > height: + cropped_size_parameters["target_width"] = width + cropped_size_parameters["target_height"] = height + elif crop_size_mode == "no smaller than mask": + if cropped_size_parameters["target_width"] < width and cropped_size_parameters["target_height"] < height: + cropped_size_parameters["target_width"] = width + cropped_size_parameters["target_height"] = height + cropped_size_parameters["batch_mode"] = False return (cropped_size_parameters, ) diff --git a/pyproject.toml b/pyproject.toml index 7c3a887..effadd7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-levelpixel" description = "Main nodes of the Level Pixel company (levelpixel, LP). In this package you will find easy-to-use atomic nodes that can help automate various processes - memory cleaning automation (RAM free, VRAM free), Lora loading like in Automatic1111, powerful inpaint crop and stitch, pipeline, declaring standard variables, converting variables, google translate, filtering tags by categories, working with file system, counting files in file system, counting objects, counting images. The names of the key nodes in this package are: Inpaint-CropAndStitch, Inpaint Crop, Inpaint Stitch, Resize Image, Resize Mask, Model Unloader, Free memory, Tag Filters, Tag Category Filters, Tag Choice Parser, File counter, Image Loader From Path (with counters), Google Translator, Language Translator, Override Device, Delay, Lora Tag Loader, Pipeline, Resize Masks, Resize Image, Iterators. Licensed under GNU GPLv3. Repository: https://github.com/LevelPixel/ComfyUI-LevelPixel" -version = "1.3.1" +version = "1.3.2" license = { file = "LICENSE.txt" } dependencies = [ "torch>=2.0.1",