KJNodes/imagegenerated

Image Crop By Mask(ImageCropByMask)

This node accepts tensor inputs of two types: `IMAGE` and `MASK`

Image Crop By Mask

image
mask
image
KJNodes

The Image Crop By Mask node receives tensor inputs of type IMAGE and MASK.

The Image Crop By Mask node is an image processing tool that automatically crops an input image based on a user-provided mask. Its core function is to identify the valid region within the mask, use this region as a bounding box, and precisely crop out the unwanted parts of the image, retaining only the content corresponding to the mask.

Node Function

This node receives tensor inputs of type IMAGE and MASK. Internally, it binarizes the mask (via the round() method) and then independently calculates the minimum bounding rectangle (i.e., the bounding box) of the non-zero pixels in the mask for each image in the input batch. Finally, the node crops the original image according to this bounding box and outputs the recombined set of all cropped images.

Node Parameter Description - Image Crop By Mask

Inputs

Parameter NameData TypeRequiredDefault ValueRange/OptionsDescription
imageIMAGEYes--image (input parameter): The original image to be cropped.
maskMASKYes--mask (input parameter): The mask used to define the cropping area. It is a MASK tensor type, has no default value, and no specific range restrictions.

Outputs

Parameter NameData TypeDescription
imageIMAGEimage (output parameter): Outputs the cropped image result.

Usage Scenarios

This node is very useful in image inpainting or local redrawing workflows. For example, after you generate a mask region for an image using VAEEncode and KSampler, you can input both the original image and the mask into this node to quickly obtain an image slice containing only the target area. This facilitates subsequent operations such as upscaling, detail enhancement, or composition with other images.

Notes

This node requires the input image (image) and mask (mask) to be compatible in the batch dimension. If the batch sizes are inconsistent, the node will reuse the last mask via the index min(b, mask.shape[0]-1), which may not be the expected behavior in all cases. Pay attention to data alignment when using this node.

The Image Crop By Mask node is from the ComfyUI-KJNodes node package.

Comments

Sign in with GitHub to join the discussion.

Loading comments…