ComfyUI Mask Area Condition
A simple custom node for ComfyUI that analyzes the size (area) of a mask relative to the total image area. Its primary purpose is to enable conditional workflows, allowing you to run face detailing only when needed based on the size of the detected face.
Why Measure Mask Size?
Image generation models often struggle with rendering small details, especially faces. While nodes like Face Detailer (part of the popular Impact Pack) or ADetailer (After Detailer) can fix this using inpainting, running them unconditionally adds significant processing time, even if the face is already large and well-rendered.
While it's possible to achieve similar conditional logic by combining existing nodes (e.g., getting mask properties and using math/comparison nodes), this dedicated MaskAreaCondition node provides a streamlined, single-node solution specifically for this common optimization task, keeping your workflow cleaner and easier to manage.
This node helps optimize such workflows by checking the mask size first.
Features
Mask Area Conditionnode:- Calculates the percentage of the mask area compared to the total tensor area.
- Outputs a boolean (
is_below_threshold) indicating if the mask percentage is less than the provided threshold. - Outputs the calculated mask size percentage (
mask_area_percent). - Passes the original mask through (
mask_passthrough) for easy chaining.
- Helper Node for Conditional Workflows:
Select Data based on Condition: Takes two data inputs (data_if_true,data_if_false) of any type and a booleancondition. It outputs eitherdata_if_trueordata_if_falsebased on the condition. This is useful for dynamically selecting workflow parameters (like KSampler steps) or routing final data (like images) based on the mask area condition.
Installation
Via ComfyUI Manager (Recommended)
- Ensure you have ComfyUI Manager installed
- Open ComfyUI and click the "Manager" button
- Navigate to "Install Custom Nodes" tab
- Search for "Mask Area Condition"
- Click "Install"
- Restart ComfyUI and reload the browser tab
Manual Installation
- Navigate to your ComfyUI
custom_nodesdirectory:cd /path/to/ComfyUI/custom_nodes/ - Clone this repository:
git clone https://github.com/a-und-b/ComfyUI_MaskAreaCondition.git - Restart ComfyUI
Usage
- Add the
Mask Area Conditionnode to your workflow (Category:mask/conditional). - Connect a
MASKoutput from another node to themaskinput. - Set the desired
threshold_percentvalue (0-100). - Implement Conditional Logic using
Select Data:- Identify the parameter you want to control based on the mask size (e.g., the
stepsinput of a KSampler for inpainting). - Create two Primitive nodes holding the different values for that parameter (e.g., one Integer node with
28for full processing, one with1for minimal processing/bypass). - Add the
Select Data based on Conditionnode. - Connect the
is_below_thresholdoutput fromMask Area Conditionto theconditioninput ofSelect Data. - Connect the Primitive node for the "condition is true" case (e.g.,
28steps) to thedata_if_trueinput. - Connect the Primitive node for the "condition is false" case (e.g.,
1step) to thedata_if_falseinput. - Connect the
selected_dataoutput ofSelect Datato the target parameter input (e.g., the KSampler'sstepsinput).
- Identify the parameter you want to control based on the mask size (e.g., the
- Handle Output Image Routing (Optional but common): If your conditional process generates a different final image (e.g., an inpainted image vs. the original), you might still need a way to select the correct final image.
- One common approach is to use a second
Select Data based on Conditionnode. - Feed the original image (or bypassed image) into
data_if_false. - Feed the processed image (e.g., inpainted image) into
data_if_true. - Use the same
is_below_thresholdboolean as thecondition. - The
selected_dataoutput will be the correct image to send toSave Image.
- One common approach is to use a second
- Optionally, use
mask_area_percentfor display or other logic, andmask_passthroughfor the actual processing step.
Example Use Cases
- Optimize Face Detailing: Automatically skip computationally expensive face detailing (like Face Detailer or After Detailer) if the detected face mask is already large enough (i.e., when
is_below_thresholdisFalse). - Conditional Image Processing: Apply different effects or processing steps based on the size of a masked object.
- Workflow Routing: Direct the workflow down different paths depending on whether a detected object meets a size criterion.
- Quality Control: Filter out or handle masks differently if they are too small or too large for subsequent reliable processing.
Acknowledgements
- Georg Neumann from KI Marketing Bootcamp for the initial code and concept
- The ComfyUI team and contributors for creating an amazing platform
- The vibrant ComfyUI community and all the custom node creators who continue to push the boundaries of what's possible
- Claude by Anthropic for assistance in code refactoring, optimization, and documentation
License
This project is licensed under the MIT License - see the LICENSE file for details.
Created with ❤️ for the ComfyUI community.