KJNodes/imagegenerated

Image Crop By Mask Batch(ImageCropByMaskBatch)

This node takes an image (IMAGE) and a mask (MASK) as input, where the mask is used to define the area to be preserved in the image.

Image Crop By Mask Batch

image
masks
images
masks
width
INT
height
INT
padding
0
preserve_size
bg_color
0, 0, 0
KJNodes

The Image Crop By Mask Batch node receives an image (IMAGE) and a mask (MASK) as input. The mask is used to define the area to be preserved in the image.

The Image Crop By Mask Batch node is an efficient image processing tool capable of batch-cropping input images based on provided masks. Its core functionality is to automatically identify the mask region and precisely extract that area from the original image, generating new images and their corresponding masks.

Node Functionality

This node receives an image (IMAGE) and a mask (MASK) as input. The mask defines the area to be preserved in the image. The node calculates the bounding box of the effective area for each mask and crops it according to the specified width, height, and padding values. If the mask dimensions do not match the image, the node automatically performs interpolation adjustments. The cropped area is placed at the center of a new canvas and supports custom background colors.

Node Parameter Description - Image Crop By Mask Batch

Connection Inputs (Inputs)

Parameter NameData TypeRequiredDefault ValueRange/OptionsDescription
imageIMAGEYes--image (Input Parameter): The original input image to be cropped.
masksMASKYes--The masks used to define the cropping region.

Control Parameters (Parameters)

Parameter NameData TypeRequiredDefault ValueRange/OptionsDescription
widthINTYes--width (Input Parameter): The width of the output image after cropping.
heightINTYes--height (Input Parameter): The height of the output image after cropping.
paddingINTYes00 - 4096 (Step: 1)padding (Input Parameter): The number of additional pixels to extend beyond the mask region's bounding box, ranging from 0 to 4096.
preserve_sizeBOOLEANYesFalse-preserve_size (Input Parameter): If True, the width and height parameters are ignored, and the output image retains its original dimensions.
bg_colorSTRINGYes0, 0, 0-Color as RGB values in range 0-255 or 0.0-1.0, or color name or hex code

Output

Parameter NameData TypeDescription
imagesIMAGEimages (Output Parameter): The collection of images generated after cropping based on the masks.
masksMASKThe cropped and adjusted masks.

Usage Scenarios

This node is highly useful in image inpainting or local repainting workflows. For example, you can first use a mask generation node (such as SAM or Mask Editor) to mark the area in the image that needs modification (e.g., a face). Then, use this node to crop out the marked area for separate detail adjustments or stylization before compositing it back into the original image.

Notes

Please note that the input mask must be a single-channel binary image, where the white (or non-zero) area represents the part to be preserved. If a mask is entirely black (i.e., has no effective area), the corresponding image will not be processed.

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

Comments

Sign in with GitHub to join the discussion.

Loading comments…