Nodes/Comfyui_PDuse/PD_Mask Selector By Area Left
ComfyUI Node

PD_Mask Selector By Area Left

Picking one object out of a sticker sheet without touching a brush

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD_Mask Selector By Area Left
  • image
  • mask
  • image
  • mask
  • x
  • y
select
padding10

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 mask1mask4 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) and mask (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

  • image and mask - the cropped region and its matching mask, ready to feed an inpainting/detailer loop or a composite.
  • x and y - 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 mask4 when 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.

CategoryPDuse/Mask

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
maskMASK
selectCOMBO4 options: mask1, mask2, mask3, mask4
paddingINT100–200

Outputs (4)

NameTypeDescription
imageIMAGE
maskMASK
xINT
yINT