PD_Mask Selector By Area Left
Picking one object out of a sticker sheet without touching a brush
- image
- mask
- image
- mask
- x
- y
You've got a sheet of sprites - an emoji grid, a sticker collage, a scanned page of separated objects - and you want one of them out, cropped and masked, to feed a detailer or a composite. That's what this node does. It looks at your mask, finds the separate blobs, sorts them, and hands you the Nth one as a clean crop with its own mask and the coordinates so you could paste it back exactly.
The name is a lie in one tiny way: it's not just "by area" - it picks the largest regions first, then orders those left to right, and mask1–mask4 indexes into that sorted list. Fine for most sheets; just don't assume mask1 is always the biggest blob on the left side of the raw mask, because it's "biggest overall, then leftmost among the top four." For a 3x3 meme grid that's exactly what you want.
How it works
Under the hood it runs OpenCV's connectedComponentsWithStats on your mask, which is the same blob-finding trick the whole detection layer leans on (see the modidex masking doc - "you can get that mask in SO many ways"). Each connected region becomes a candidate with an area, bounding box, and centroid. The node keeps the four biggest, sorts them by centroid x so the leftmost region is mask1, then crops the one you asked for. The crop is padded outward by padding pixels so you don't slice off ears or halos, and everything is repacked into a uniform batch sized to the largest crop.
The inputs that matter
image(IMAGE) andmask(MASK) - feed it a Load Image plus its mask. White in the mask (> 0.5) is treated as foreground.select-mask1/mask2/mask3/mask4, which region to pull.padding(0–200, default 10) - extra border around the crop. Bump it if the crop is clipping the subject.
What comes out
imageandmask- the cropped region and its matching mask, ready to feed an inpainting/detailer loop or a composite.xandy- the crop's top-left corner in the original image, so you can paste the result back or sync another crop node to the same spot.
Installing it
This ships in the Comfyui_PDuse pack by 7BEII. Easiest is ComfyUI Manager: search "PDuse" and hit Install. Or do it by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/7BEII/Comfyui_PDuse.git
cd Comfyui_PDuse
pip install -r requirements.txt
Restart ComfyUI. The pack pulls in opencv-python, scipy, numpy, Pillow, and torchvision - no model downloads, it's pure utility code. Caveat: the author's docs are mostly in Chinese (Feishu wiki, Bilibili walkthrough), so the English you'll find is what the node tooltips give you.
Where people get burned
- Fewer than four regions. Ask for
mask4when the mask only has two blobs and you get a blank 1x1 crop instead of an error. Silent and confusing - check your report by counting blobs first. - The sort order. It's area first, then left-to-right among the top four. If your "leftmost" blob is small, it won't be
mask1. - It does nothing to the mask. If your blobs are touching, they're one region, not two. Erode or threshold upstream to separate them before this node.
Backup anything you're feeding in. The node never writes files, but you'll have a much easier morning if your source mask is already clean.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| select | COMBO | 4 options: mask1, mask2, mask3, mask4 | |
| padding | INT | 100–200 | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |
| x | INT | — |
| y | INT | — |