ComfyUI > Nodes > ComfyUI Layer Style > LayerUtility: CropByMask V2

ComfyUI Node: LayerUtility: CropByMask V2

Class Name

LayerUtility: CropByMask V2

Category
😺dzNodes/LayerUtility
Author
chflame163 (Account age: 445days)
Extension
ComfyUI Layer Style
Latest Updated
2024-06-24
Github Stars
0.64K

How to Install ComfyUI Layer Style

Install this extension via the ComfyUI Manager by searching for ComfyUI Layer Style
  • 1. Click the Manager button in the main menu
  • 2. Select Custom Nodes Manager button
  • 3. Enter ComfyUI Layer Style in the search bar
After installation, click the Restart button to restart ComfyUI. Then, manually refresh your browser to clear the cache and access the updated list of nodes.

Visit ComfyUI Online for ready-to-use ComfyUI environment

  • Free trial available
  • High-speed GPU machines
  • 200+ preloaded models/nodes
  • Freedom to upload custom models/nodes
  • 50+ ready-to-run workflows
  • 100% private workspace with up to 200GB storage
  • Dedicated Support

Run ComfyUI Online

LayerUtility: CropByMask V2 Description

Powerful node for precise image cropping based on masks, ideal for AI artists and advanced detection methods.

LayerUtility: CropByMask V2:

LayerUtility: CropByMask V2 is a powerful node designed to crop images based on a specified mask. This node is particularly useful for AI artists who need to isolate specific regions of an image for further processing or analysis. By leveraging advanced detection methods, such as minimum bounding rectangle and maximum inscribed rectangle, this node ensures precise cropping around the masked area. Additionally, it offers flexibility with parameters like invert mask, reserve margins, and rounding to multiples, allowing you to fine-tune the cropping process to meet your specific needs. The main goal of this node is to provide a seamless and efficient way to crop images accurately, enhancing your workflow and enabling more focused image manipulation.

LayerUtility: CropByMask V2 Input Parameters:

image

This parameter represents the input image that you want to crop. It should be provided in a tensor format, which is a common data structure used in image processing and machine learning.

mask

The mask parameter is a binary or grayscale image that defines the area to be cropped. The non-zero regions of the mask indicate the parts of the image that should be retained after cropping.

invert_mask

This boolean parameter determines whether the mask should be inverted before processing. If set to True, the areas of the image corresponding to the zero regions of the mask will be retained, and vice versa. This is useful when you need to crop out the background or specific unwanted regions.

detect

The detect parameter specifies the method used to determine the cropping area. It can take values like "min_bounding_rect" for the minimum bounding rectangle or "max_inscribed_rect" for the maximum inscribed rectangle. This allows you to choose the most suitable detection method for your specific use case.

top_reserve

This parameter defines the number of pixels to reserve at the top of the cropped area. It ensures that a margin is maintained above the detected region, which can be useful for preserving context or avoiding tight crops. The value should be a non-negative integer.

bottom_reserve

Similar to top_reserve, this parameter specifies the number of pixels to reserve at the bottom of the cropped area. It helps in maintaining a margin below the detected region. The value should be a non-negative integer.

left_reserve

This parameter sets the number of pixels to reserve on the left side of the cropped area. It ensures that a margin is maintained to the left of the detected region. The value should be a non-negative integer.

right_reserve

Similar to left_reserve, this parameter defines the number of pixels to reserve on the right side of the cropped area. It helps in maintaining a margin to the right of the detected region. The value should be a non-negative integer.

round_to_multiple

This parameter allows you to round the dimensions of the cropped area to the nearest multiple of a specified value. This is useful for ensuring that the cropped dimensions are compatible with certain processing requirements or constraints. The value should be a positive integer or 'None' if no rounding is needed.

crop_box

The crop_box parameter is an optional input that allows you to specify a predefined cropping box. If provided, the node will use this box for cropping instead of calculating it based on the mask. The value should be a tuple of four integers representing the coordinates of the box (x1, y1, x2, y2).

LayerUtility: CropByMask V2 Output Parameters:

cropped_image

This output parameter provides the cropped image based on the specified mask and other input parameters. The cropped image is returned in a tensor format, ready for further processing or analysis.

cropped_mask

The cropped_mask parameter returns the mask corresponding to the cropped image. This is useful for maintaining the relationship between the image and its mask after cropping. The cropped mask is also provided in a tensor format.

crop_box

This output parameter returns the coordinates of the cropping box used for the operation. It is a tuple of four integers (x1, y1, x2, y2) that represent the boundaries of the cropped area. This information can be useful for reference or further processing.

preview_image

The preview_image parameter provides a visual representation of the cropping process. It includes the original image with the detected cropping box overlaid, allowing you to verify the accuracy of the cropping operation. The preview image is returned in a tensor format.

LayerUtility: CropByMask V2 Usage Tips:

  • To achieve precise cropping, ensure that your mask accurately represents the area you want to retain. Fine-tune the mask if necessary to avoid unwanted regions.
  • Use the reserve parameters (top_reserve, bottom_reserve, left_reserve, right_reserve) to maintain margins around the cropped area, which can be useful for preserving context or avoiding tight crops.
  • Experiment with different detection methods (min_bounding_rect, max_inscribed_rect) to find the one that best suits your specific use case and provides the most accurate cropping results.
  • If you need the cropped dimensions to be compatible with certain processing requirements, use the round_to_multiple parameter to round the dimensions to the nearest multiple of a specified value.

LayerUtility: CropByMask V2 Common Errors and Solutions:

"Invalid mask format"

  • Explanation: This error occurs when the provided mask is not in a valid format (binary or grayscale).
  • Solution: Ensure that the mask is a binary or grayscale image and is provided in the correct format.

"Invalid detection method"

  • Explanation: This error occurs when an unsupported detection method is specified in the detect parameter.
  • Solution: Verify that the detect parameter is set to a supported value, such as "min_bounding_rect" or "max_inscribed_rect".

"Crop box out of bounds"

  • Explanation: This error occurs when the specified crop_box coordinates are outside the dimensions of the input image.
  • Solution: Ensure that the crop_box coordinates are within the bounds of the input image dimensions.

"Negative reserve value"

  • Explanation: This error occurs when a negative value is provided for one of the reserve parameters (top_reserve, bottom_reserve, left_reserve, right_reserve).
  • Solution: Ensure that all reserve parameters are set to non-negative integer values.

LayerUtility: CropByMask V2 Related Nodes

Go back to the extension to check out more related nodes.
ComfyUI Layer Style
RunComfy

© Copyright 2024 RunComfy. All Rights Reserved.

RunComfy is the premier ComfyUI platform, offering ComfyUI online environment and services, along with ComfyUI workflows featuring stunning visuals.