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
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 Name | Data Type | Required | Default Value | Value Range/Options | Description |
|---|---|---|---|---|---|
image | IMAGE | Yes | - | - | image (input parameter): The original input image to be cropped. |
mask | MASK | Yes | - | - | 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 Name | Data Type | Required | Default Value | Value Range/Options | Description |
|---|---|---|---|---|---|
base_resolution | INT | Yes | 512 | 0 - 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. |
padding | INT | Yes | 0 | 0 - 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_resolution | INT | Yes | 128 | 0 - 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_resolution | INT | Yes | 512 | 0 - 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 Name | Data Type | Description |
|---|---|---|
| images | IMAGE | images (output parameter): The output image after cropping and scaling. |
| masks | MASK | The mask after undergoing the same cropping and scaling process. |
| bbox | BBOX | bbox (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.
Image Crop By Mask And Resize Node Source Code Link
The Image Crop By Mask And Resize node is from the ComfyUI-KJNodes node package.
Comments
Sign in with GitHub to join the discussion.