File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1313)
1414from invokeai .app .invocations .constants import IMAGE_MODES
1515from 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+
You can’t perform that action at this time.
0 commit comments