Nodes/comfyui-tensorop/GetLayerMask
ComfyUI Node

GetLayerMask

Turn a flat-color layer render into real per-layer masks

By un-seen·Created 2 years ago·Updated 2 years ago· 46
GetLayerMask
  • image
  • json_data
  • MASK
  • MASK
  • JSON

GetLayerMask is the node that turns this pack's detection output into masks you can actually use. Give it an image and the pack's JSON, and it returns three things: a batch of color-based layer masks, a batch of text-region masks, and the list of text labels. If you're compositing a segmented scene or preparing layers for inpainting, this is the payoff node - the one that turns "a JSON blob from detection" into wires you can plug into anything.

The catch, and it's a real one: it assumes the image is a flat-color layer render, not a photograph. The mechanism is entirely color-based. It finds every unique color in the image, builds a binary mask for each one, and treats the largest color region as the background - which it then subtracts from everything. Pure black is skipped entirely. The output's first MASK is that background mask stacked together with one mask per remaining color. So this works on a segmentation render where each object got painted a distinct solid color, not on a photo with gradients and textures.

Where the text masks come from

The json_data input is a list of items, each with a polygon (flat x, y coordinates) and a label. For every item, the node draws the polygon as a white shape on a black grayscale mask. Those all get stacked into the second MASK output, and the labels come out as the third output. In this pack's pipeline that JSON comes from Florence-2 OCR - detected text boxes with their transcriptions - and the color layers come from SAM2 segmentation. You get a background mask, per-element masks, per-text-region masks, and the text itself, all ready to feed inpainting, compositing, or a region-controlled sampler.

Inputs and outputs

  • image (IMAGE) - the flat-color composite you're splitting into layers.
  • json_data (JSON) - items with polygon and label fields for the text regions.
  • Output 1: MASK - background mask + one mask per unique non-black color, stacked as a batch.
  • Output 2: MASK - text-region masks, stacked.
  • Output 3: JSON - the list of text labels, in the same order as the text masks.

The stack of masks in output 1 is the thing you'll actually iterate over or select from - feed the batch to a mask-select node or use them one at a time for per-layer edits.

Installing it

Same pack, same drill. ComfyUI Manager, search comfyui-tensorop, or:

cd ComfyUI/custom_nodes
git clone https://github.com/un-seen/comfyui-tensorops
cd comfyui-tensorops
pip install -r requirements.txt

Restart. This node only needs numpy/PIL/torch, but the pack's requirements install the heavy stuff too - and the README is empty, so the source code is the only real documentation.

Where people get burned

  • Photographs in, garbage out. The color-mask logic needs flat, distinct colors. Feed it a real photo and you'll get a mask per shade - unusable noise.
  • Background = biggest region. If the largest color area isn't actually the background, it gets removed anyway. It's a heuristic, not a label.
  • Manager conflict warnings. The pack shares node names with Kijai's ComfyUI-Florence2, so watch for the clash warning if you install both.

If you're running this pack's Florence-2 + SAM2 pipeline, GetLayerMask is where the pieces come together. Standalone, it's a niche but genuinely clever way to turn a palette render into compositable layers.

Categorytensorops

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
json_dataJSON

Outputs (3)

NameTypeDescription
MASKMASK
MASKMASK
JSONJSON