Skip to content

Commit e0edfe6

Browse files
feat(nodes): add CropImageToBoundingBoxInvocation
1 parent 8a0a371 commit e0edfe6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

invokeai/app/invocations/image.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
from invokeai.app.invocations.constants import IMAGE_MODES
1515
from invokeai.app.invocations.fields import (
16+
BoundingBoxField,
1617
ColorField,
1718
FieldDescriptions,
1819
ImageField,
@@ -1138,3 +1139,30 @@ def invoke(self, context: InvocationContext) -> ImageOutput:
11381139
image_dto = context.images.save(image=noisy_image)
11391140

11401141
return ImageOutput.build(image_dto)
1142+
1143+
1144+
@invocation(
1145+
"crop_image_to_bounding_box",
1146+
title="Crop Image to Bounding Box",
1147+
category="image",
1148+
version="1.0.0",
1149+
tags=["image", "crop"],
1150+
)
1151+
class CropImageToBoundingBoxInvocation(BaseInvocation, WithMetadata, WithBoard):
1152+
"""Crop an image to the given bounding box. If the bounding box is omitted, the image is cropped to the non-transparent pixels."""
1153+
1154+
image: ImageField = InputField(description="The image to crop")
1155+
bounding_box: BoundingBoxField | None = InputField(
1156+
default=None, description="The bounding box to crop the image to"
1157+
)
1158+
1159+
def invoke(self, context: InvocationContext) -> ImageOutput:
1160+
image = context.images.get_pil(self.image.image_name)
1161+
1162+
bounding_box = self.bounding_box.tuple() if self.bounding_box is not None else image.getbbox()
1163+
1164+
cropped_image = image.crop(bounding_box)
1165+
1166+
image_dto = context.images.save(image=cropped_image)
1167+
return ImageOutput.build(image_dto)
1168+

0 commit comments

Comments
 (0)