diff --git a/__init__.py b/__init__.py index 66a58a1..70db11e 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,5 @@ from .modules.image_loader import CachedLoadImageFromUrl -from .modules.image_tool import OFFCenterCrop, OFFCenterCropSEGS, OFFSEGSToImage, OFFImageResizeFit, OFFWatermark, MaskToImageFallback, MaskDilationForEachFace, SegsToFaceCropData, PasteFaceSegToImage, OFFCLAHE,OffGridImageBatch +from .modules.image_tool import OFFCenterCrop, OFFCenterCropSEGS, OFFSEGSToImage, OFFImageResizeFit,OFFWatermark, MaskToImageFallback, MaskDilationForEachFace, SegsToFaceCropData, PasteFaceSegToImage, OFFCLAHE,OffGridImageBatch, CalcMaskBound from .modules.misc import GWNumFormatter, QueryGenderAge, RandomSeedFromList from .modules.latent_tool import VAEEncodeForInpaintV2 @@ -23,7 +23,8 @@ NODE_CLASS_MAPPINGS = { "Paste Face Segment to Image" : PasteFaceSegToImage, "Apply CLAHE" : OFFCLAHE, "Grid Image from batch (OFF)" : OffGridImageBatch, - "RandomSeedfromList" : RandomSeedFromList + "RandomSeedfromList" : RandomSeedFromList, + "CalcMaskBound":CalcMaskBound } # A dictionary that contains the friendly/humanly readable titles for the nodes @@ -44,5 +45,6 @@ NODE_DISPLAY_NAME_MAPPINGS = { "Paste Face Segment to Image":"Paste Face Segment to Image", "Apply CLAHE":"Apply CLAHE", "Grid Image from batch (OFF)" : "Grid Image from batch (OFF)", - "RandomSeedfromList" : "Random Seed from List" + "RandomSeedfromList" : "Random Seed from List", + "CalcMaskBound": "Calculate mask bound" } diff --git a/modules/image_tool.py b/modules/image_tool.py index 00863a0..d312da5 100644 --- a/modules/image_tool.py +++ b/modules/image_tool.py @@ -482,3 +482,56 @@ class OffGridImageBatch: new_image = ImageOps.expand(new_image, border=outer_border_width, fill=border_color) return (pil2tensor(new_image), ) + +class CalcMaskBound: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "mask": ("MASK",), + "padding":("INT",{"default":0, "min": 0, "step":1}) + } + } + + CATEGORY = "OFF" + + RETURN_TYPES = ("INT","INT","INT","INT",) + FUNCTION = "process" + + def process(self, mask, padding): + + rows, cols = np.where(mask[0]) + min_row, max_row = rows.min(), rows.max() + min_col, max_col = cols.min(), cols.max() + + min_col -= padding + min_row -= padding + + max_col += padding + max_row += padding + + if min_col< 0 : + min_col = 0 + + if min_row<0: + min_row = 0 + + if max_row > mask.shape[1]: + max_row = mask.shape[1] + + if max_col > mask.shape[2]: + max_col = mask.shape[2] + + + return (min_col, min_row, max_col - min_col, max_row - min_row, ) + + + + + + + +