Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

BBox widget (#11594)

Terry Jia8ad38d2073b0
diff --git a/comfy_api/latest/_io.py b/comfy_api/latest/_io.py
index d18330d0b..312681249 100644
--- a/comfy_api/latest/_io.py
+++ b/comfy_api/latest/_io.py
@@ -1209,6 +1209,30 @@ class Color(ComfyTypeIO):
def as_dict(self):
return super().as_dict()
+@comfytype(io_type="BOUNDING_BOX")
+class BoundingBox(ComfyTypeIO):
+ class BoundingBoxDict(TypedDict):
+ x: int
+ y: int
+ width: int
+ height: int
+ Type = BoundingBoxDict
+
+ class Input(WidgetInput):
+ def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None,
+ socketless: bool=True, default: dict=None, component: str=None):
+ super().__init__(id, display_name, optional, tooltip, None, default, socketless)
+ self.component = component
+ if default is None:
+ self.default = {"x": 0, "y": 0, "width": 512, "height": 512}
+
+ def as_dict(self):
+ d = super().as_dict()
+ if self.component:
+ d["component"] = self.component
+ return d
+
+
DYNAMIC_INPUT_LOOKUP: dict[str, Callable[[dict[str, Any], dict[str, Any], tuple[str, dict[str, Any]], str, list[str] | None], None]] = {}
def register_dynamic_input_func(io_type: str, func: Callable[[dict[str, Any], dict[str, Any], tuple[str, dict[str, Any]], str, list[str] | None], None]):
DYNAMIC_INPUT_LOOKUP[io_type] = func
@@ -2190,5 +2214,6 @@ __all__ = [
"ImageCompare",
"PriceBadgeDepends",
"PriceBadge",
+ "BoundingBox",
"NodeReplace",
]
diff --git a/comfy_extras/nodes_images.py b/comfy_extras/nodes_images.py
index cb4fb24a1..23419a65d 100644
--- a/comfy_extras/nodes_images.py
+++ b/comfy_extras/nodes_images.py
@@ -23,8 +23,9 @@ class ImageCrop(IO.ComfyNode):
return IO.Schema(
node_id="ImageCrop",
search_aliases=["trim"],
- display_name="Image Crop",
+ display_name="Image Crop (Deprecated)",
category="image/transform",
+ is_deprecated=True,
inputs=[
IO.Image.Input("image"),
IO.Int.Input("width", default=512, min=1, max=nodes.MAX_RESOLUTION, step=1),
@@ -47,6 +48,57 @@ class ImageCrop(IO.ComfyNode):
crop = execute # TODO: remove
+class ImageCropV2(IO.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return IO.Schema(
+ node_id="ImageCropV2",
+ search_aliases=["trim"],
+ display_name="Image Crop",
+ category="image/transform",
+ inputs=[
+ IO.Image.Input("image"),
+ IO.BoundingBox.Input("crop_region", component="ImageCrop"),
+ ],
+ outputs=[IO.Image.Output()],
+ )
+
+ @classmethod
+ def execute(cls, image, crop_region) -> IO.NodeOutput:
+ x = crop_region.get("x", 0)
+ y = crop_region.get("y", 0)
+ width = crop_region.get("width", 512)
+ height = crop_region.get("height", 512)
+
+ x = min(x, image.shape[2] - 1)
+ y = min(y, image.shape[1] - 1)
+ to_x = width + x
+ to_y = height + y
+ img = image[:,y:to_y, x:to_x, :]
+ return IO.NodeOutput(img, ui=UI.PreviewImage(img))
+
+
+class BoundingBox(IO.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return IO.Schema(
+ node_id="PrimitiveBoundingBox",
+ display_name="Bounding Box",
+ category="utils/primitive",
+ inputs=[
+ IO.Int.Input("x", default=0, min=0, max=MAX_RESOLUTION),
+ IO.Int.Input("y", default=0, min=0, max=MAX_RESOLUTION),
+ IO.Int.Input("width", default=512, min=1, max=MAX_RESOLUTION),
+ IO.Int.Input("height", default=512, min=1, max=MAX_RESOLUTION),
+ ],
+ outputs=[IO.BoundingBox.Output()],
+ )
+
+ @classmethod
+ def execute(cls, x, y, width, height) -> IO.NodeOutput:
+ return IO.NodeOutput({"x": x, "y": y, "width": width, "height": height})
+
+
class RepeatImageBatch(IO.ComfyNode):
@classmethod
def define_schema(cls):
@@ -632,6 +684,8 @@ class ImagesExtension(ComfyExtension):
async def get_node_list(self) -> list[type[IO.ComfyNode]]:
return [
ImageCrop,
+ ImageCropV2,
+ BoundingBox,
RepeatImageBatch,
ImageFromBatch,
ImageAddNoise,