Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(nodes): return an exclusive x_max/y_max from Get Image Mask Bounding Box (#9603)
Mohammad Hijjawi89a01d1f7674
diff --git a/invokeai/app/invocations/mask.py b/invokeai/app/invocations/mask.pyindex 49749f43b6..37288e62f0 100644--- a/invokeai/app/invocations/mask.py+++ b/invokeai/app/invocations/mask.py@@ -253,7 +253,8 @@ class GetMaskBoundingBoxInvocation(BaseInvocation):# No pixels found with the given colorreturn BoundingBoxOutput(bounding_box=BoundingBoxField(x_min=0, y_min=0, x_max=0, y_max=0))- left, upper, right, lower = x.min(), y.min(), x.max(), y.max()+ # x_max and y_max are exclusive, so they sit one past the last mask pixel+ left, upper, right, lower = x.min(), y.min(), x.max() + 1, y.max() + 1# Add the marginleft = max(0, left - self.margin)diff --git a/tests/app/invocations/test_mask_bounding_box.py b/tests/app/invocations/test_mask_bounding_box.pynew file mode 100644index 0000000000..a14e5edfa0--- /dev/null+++ b/tests/app/invocations/test_mask_bounding_box.py@@ -0,0 +1,48 @@+from unittest.mock import MagicMock++import pytest+from PIL import Image++from invokeai.app.invocations.fields import ImageField+from invokeai.app.invocations.mask import GetMaskBoundingBoxInvocation+++def _build_context(mask: Image.Image) -> MagicMock:+ context = MagicMock()+ context.images.get_pil.return_value = mask+ return context+++ ("margin", "expected"),+ [+ (0, (2, 1, 5, 3)),+ (1, (1, 0, 6, 4)),+ # The margin is clamped to the image bounds, which for x_max / y_max is the image size.+ (10, (0, 0, 8, 6)),+ ],+)+def test_get_mask_bounding_box_max_coords_are_exclusive(margin: int, expected: tuple[int, int, int, int]) -> None:+ mask = Image.new("RGBA", (8, 6), (0, 0, 0, 255))+ # Mask pixels span x in [2, 4] and y in [1, 2].+ for x in range(2, 5):+ for y in range(1, 3):+ mask.putpixel((x, y), (255, 255, 255, 255))++ output = GetMaskBoundingBoxInvocation(mask=ImageField(image_name="mask"), margin=margin).invoke(+ _build_context(mask)+ )++ assert output.bounding_box.tuple() == expected+ # Cropping to the bounding box (as "Crop Image to Bounding Box" does) keeps every mask pixel.+ if margin == 0:+ assert mask.crop(output.bounding_box.tuple()).getcolors() == [(6, (255, 255, 255, 255))]+++def test_get_mask_bounding_box_single_pixel() -> None:+ mask = Image.new("RGBA", (4, 4), (0, 0, 0, 255))+ mask.putpixel((1, 1), (255, 255, 255, 255))++ output = GetMaskBoundingBoxInvocation(mask=ImageField(image_name="mask")).invoke(_build_context(mask))++ assert output.bounding_box.tuple() == (1, 1, 2, 2)