LayerStyle

2167
By chflame
Updated 15 days ago
View on GitHub →See Common Issues →

A set of nodes for ComfyUI that can composite layer and mask to achieve Photoshop like functionality.

Available Nodes

LayerUtility: CropByMask V3

LayerUtility: CropByMask V3 Documentation

Overview

The LayerUtility: CropByMask V3 node is part of the ComfyUI LayerStyle utility collection. This node is designed to process images by cropping them based on a given mask. It provides a flexible mechanism to isolate or extract specific parts of an image by using a mask that defines the region of interest. The node accommodates various modes of detecting the area to be cropped and allows for adjustments to the cropped region through reserve margins and dimensions rounded to specific multiples.

Inputs

The node accepts the following inputs:

  • image (IMAGE): This input is the primary image or set of images you intend to crop.
  • mask (MASK): A mask defines the area of interest to be cropped in the image. It must correlate in dimensions with the input image.
  • invert_mask (BOOLEAN): This option allows the inversion of the mask to select the opposite area for cropping. The default setting is False.
  • detect (OPTIONS): It determines how to detect the cropping area within the mask. Options include:
    • mask_area: Uses the entire masked area.
    • min_bounding_rect: Crops to the minimal bounding rectangle around the mask.
    • max_inscribed_rect: Crops to the largest inscribed rectangle within the mask.
  • top_reserve (INT): Determines the number of additional pixels to reserve at the top of the detected area. Default is 20.
  • bottom_reserve (INT): Similar to top_reserve, but applies to the bottom of the detected area. Default is 20.
  • left_reserve (INT): Similar to top_reserve, but applies to the left of the detected area. Default is 20.
  • right_reserve (INT): Similar to top_reserve, but applies to the right of the detected area. Default is 20.
  • round_to_multiple (OPTIONS): Rounds the dimensions of the crop box to the nearest multiple, which can be one of 8, 16, 32, 64, 128, 256, 512, None.

Additionally, there is an optional input:

  • crop_box (BOX): You can optionally specify a crop box manually if automatic detection is not desired.

Outputs

The LayerUtility: CropByMask V3 node generates the following outputs:

  • croped_image (IMAGE): The image(s) cropped according to the mask and specified parameters.
  • croped_mask (MASK): The cropped area of the mask which aligns with the cropped image.
  • crop_box (BOX): The coordinates and dimensions of the established crop box.
  • box_preview (IMAGE): A preview image displaying the original image with overlays showing the crop box and mask boundaries, helpful for visualization.

Usage

This node is useful in workflows where targeted image manipulation based on regions of interest is necessary. For instance:

  1. Image Preprocessing: Prepare and preprocess images for further analysis by focusing on specific parts, like faces in portrait images.
  2. Background Removal: Extract objects from an image by defining what should remain or be discarded with a mask.
  3. Image Editing: Isolate and convey certain image parts for advanced compositing or special effects.

To effectively incorporate this node in your workflow, connect the images and masks according to your objectives, choose appropriate detection and reserve settings, and determine if dimensions should conform to specific multiples. These allow for precision in defining the outcome.

Special Features and Considerations

  • Reserve Margins: You can expand or shrink the crop area through reserves, thereby fine-tuning the output image dimensions beyond just the raw mask area.
  • Multiple Image Compatibility: If handling multiple images, the node processes each one systematically, ensuring efficient batch operation.
  • Output Versatility: Offers both cropped outputs and a visual preview, facilitating verification and adjustment.
  • Rounding Capability: The ability to round the crop dimensions to designated multiples aids in creating uniformly-sized images which can be useful for downstream applications that require specific aspect ratios or sizes.

It's important to note that while the node can process multiple images, only the first mask in the input is used for detection. Therefore, ensure that the mask appropriately corresponds to each image in batch operations.