KJNodes/imagegenerated

Image Crop By Mask And Resize(ImageCropByMaskAndResize)

This node first analyzes the input mask, locates all pixels with a value of 1, and calculates the minimum rectangular bounding box that can fully enclose these points.

Image Crop By Mask And Resize

image
mask
images
masks
bbox
base_resolution
INT
padding
INT
min_crop_resolution
INT
max_crop_resolution
INT
KJNodes

The Image Crop By Mask And Resize node first analyzes the input mask, locates all pixels with a value of 1, and calculates the minimum rectangular bounding box that completely encloses these points.

The Image Crop By Mask And Resize node is an image processing tool that can automatically identify and crop the corresponding area in an image based on the mask you provide. After cropping, the node will resize the area to the specified resolution and output the processed image, mask, and coordinate information of the cropped region.

Node Functionality

This node first analyzes the input mask, locates all pixels with a value of 1, and calculates the minimum rectangular bounding box that completely encloses these points. Then, it adjusts and expands this bounding box based on parameters you set, such as the minimum/maximum crop resolution and padding. Finally, based on the base_resolution you set, the node proportionally scales the cropped image and mask region to that size.

Node Parameter Description - Image Crop By Mask And Resize

Connection Inputs (Inputs)

Parameter NameData TypeRequiredDefault ValueValue Range/OptionsDescription
imageIMAGEYes--image (input parameter): The original input image to be cropped.
maskMASKYes--mask (input parameter): The mask used to define the cropping area. The white area (value 1) indicates the part to be retained.

Control Parameters (Parameters)

Parameter NameData TypeRequiredDefault ValueValue Range/OptionsDescription
base_resolutionINTYes5120 - MAX_RESOLUTION (step: 8)base_resolution (input parameter): The target resolution side length to which the cropped region will ultimately be scaled. The value range is 0 to MAX_RESOLUTION, with an adjustment step of 8.
paddingINTYes00 - MAX_RESOLUTION (step: 1)padding (input parameter): The extra pixel margin added around the calculated crop box. The value range is 0 to MAX_RESOLUTION, with an adjustment step of 1.
min_crop_resolutionINTYes1280 - MAX_RESOLUTION (step: 8)min_crop_resolution (input parameter): Ensures the minimum width and height of the crop box are not less than this value. The value range is 0 to MAX_RESOLUTION, with a step of 8.
max_crop_resolutionINTYes5120 - MAX_RESOLUTION (step: 8)max_crop_resolution (input parameter): Limits the maximum width and height of the crop box to not exceed this value. The value range is 0 to MAX_RESOLUTION, with a step of 8.

Output

Parameter NameData TypeDescription
imagesIMAGEimages (output parameter): The output image after cropping and scaling.
masksMASKThe mask after undergoing the same cropping and scaling process.
bboxBBOXbbox (output parameter): The coordinate information of the crop region's bounding box in the original image coordinate system.

Use Cases

This node is very suitable for workflows that require focusing on specific parts of an image for detailed processing. For example, in a portrait retouching workflow, you can first use a mask to select the face, then use this node to crop and enlarge the facial area separately. This allows for subsequent finer facial feature adjustments or high-definition restoration without needing to process the entire image.

Notes

Please note that if the mask area extends beyond the boundaries of the original image, or if the calculated crop box size is larger than the original image, the node may report an error. Additionally, all resolution parameters (such as base_resolution) are typically adjusted in steps of 8 to ensure compatibility with the optimal input sizes of many AI models.

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

Comments

Sign in with GitHub to join the discussion.

Loading comments…