Nodes/draw_tools/Detect Inner Box
ComfyUI Node

Detect Inner Box

How big is that cutout? This node measures it for you (in multiples of 8)

By Ky11le·Created about a year ago·Updated about a year ago· 1
Detect Inner Box
  • mask
  • width
  • height

Here's the situation DetectInnerBox exists for: you've got a card frame with a hole cut out of it, you want to generate art to fill that hole, and you need to know the hole's exact size to feed into an Empty Latent Image. Measuring it by eye and eyeballing the numbers means you'll be fighting the latent-size checker for the rest of the afternoon. This node does the measuring for you and rounds the answer to a multiple of 8, which is the one thing ComfyUI's latent space actually cares about.

It's the first half of a two-node compositing flow in the draw_tools (a.k.a. ygo_tools) pack, and yes, the "ygo" is Yu-Gi-Oh - this is a card-art pipeline. You point DetectInnerBox at a mask where the white blob is your cutout, it hands you a width and height, you generate art at that size, and the pack's other node, Paste Into Frame, drops it back into the hole. This is the crop-and-stitch style of masked compositing the inpainting folks swear by: render the region at full resolution, then composite it back bit-perfectly. Nothing else in the frame moves.

How it works

Under the hood it's a short OpenCV pipeline, and it's simpler than the README makes it sound. The README promises threshold and connectivity inputs - those don't exist in the code. It takes exactly one input, mask, and does this:

  1. Converts the mask tensor to grayscale and binarizes it with a hardcoded threshold of 250 (so only nearly-pure-white pixels count).
  2. Runs connectedComponentsWithStats with 8-connectivity to find distinct white blobs.
  3. Takes the largest blob and computes its bounding box.
  4. Rounds the width and height up to the nearest multiple of 8.

Why the rounding? ComfyUI latent tensors need dimensions divisible by 8 (some models want 16 or 64). If you wire these raw numbers into an Empty Latent Image, width = 8-rounding means you never get that cryptic dimension error, and Paste Into Frame center-crops to the real hole size anyway, so the extra pixels are harmless.

The inputs and outputs that matter

There's one input: mask (MASK). White is what you're measuring - the brighter and cleaner the blob, the better, because of that 250 threshold. If you only have the mask as an image, run it through ComfyUI's built-in Image → Mask node first.

Outputs: width (INT) and height (INT). Both come from the largest white region. Wire them into the width/height slots of an Empty Latent Image and generate.

How to install it

In ComfyUI Manager, search for draw_tools (that's the pack title). Or clone it by hand:

cd ComfyUI/custom_nodes
git clone https://github.com/Ky11le/ygo_tools
# restart ComfyUI

Heads-up: the README still says to clone Ky11le/draw_tools, which is the old repo name and 404s - use the ygo_tools URL above. The only real dependency is opencv-python>=4.5.0 (torch and numpy you already have; there are no model downloads). The nodes appear under the DrawTools category. If opencv isn't installed, run pip install -r requirements.txt inside the folder.

Troubleshooting

  • You get 0 × 0. That means no white blob survived the threshold - your mask is too dark or too soft. Remask so the region is solid white, or brighten it with a contrast node before feeding it in.
  • Multiple blobs in the mask? Only the largest one is measured. That's usually what you want for a single card window; if your frame has two holes, you'll need separate masks.
  • Generated image is bigger than the hole. Expected, by design - the rounding adds a few pixels, and Paste Into Frame trims them.

It's a tiny node, and it does one job. But it's the difference between guessing card-window dimensions and knowing them.

CategoryDrawTools

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (2)

NameTypeDescription
widthINT
heightINT