Nodes/comfyui-panels/Polygon To Resized Mask
ComfyUI Node

Polygon To Resized Mask

The panel-to-mask node that keeps your crops consistent

By bmad4ever·Created 12 months ago·Updated 2 months ago· 41
Polygon To Resized Mask
  • polygon
  • image
  • mask
  • bbox
  • image
  • scale
approx_res1048576
pad64

Polygon To Resized Mask is the node the pack's own README points to when you're doing the "generate each panel separately, paste it back" workflow. It converts a panel polygon into a mask that's been resized and padded to tidy dimensions - and it hands you the exact bookkeeping numbers you need to put the generated panel back where it belongs. For comic page assembly, this is the connective tissue.

The workflow this serves: you've built your page as a list of polygon panels. You crop each panel region out of the page, generate art inside each crop (or inpaint into it), and then paste the results back onto the page. That pipeline only works if every panel's crop, mask, and paste position are consistent. This node is the "make them consistent" step, and it's the recommended one for Paste Crops with Masks precisely because it returns the bbox and scale you need downstream.

How it works: it rasterizes the polygon to a mask at its actual pixel bounds, then scales the largest dimension so the total area lands near an approx_res target, snaps that dimension to a multiple of pad, and pads the smaller dimension out to a centered multiple too. The output mask is therefore a fixed, divisible size that resizes cleanly and plays well with the bbox math in the crop/paste nodes.

Inputs worth understanding:

  • polygon - the panel (shapely polygon) to convert.
  • approx_res - enum of 262144, 1048576, or 1638400 (default 1048576, i.e. roughly a 1024×1024 worth of pixels). Higher = crisper mask edges and higher-res crops, at the cost of more VRAM through the generation step.
  • pad - enum of 16, 32, 64, 128 (default 64). The mask dimensions land on multiples of this. 64 is a solid default.
  • image (optional) - if you feed the full page image in, you get back a crop of it taken at the polygon's bounds, resized and padded identically to the mask. The tooltip says it plainly: mainly for editing workflows, and it's genuinely handy when you want to inpaint a panel using the surrounding context.

The outputs are the whole point, so here they are:

  • mask - the resized, padded mask.
  • bbox - the mask's bounds ignoring the padding, i.e. where the panel actually is.
  • image - the matching resized crop if you provided one, else None.
  • scale - the scaling factor applied, which is exactly what you need to keep coordinates honest between the polygon world and the mask world.

The one trap to internalize: the panel you feed in should be in the same coordinate space as the image you eventually paste into, because the output bbox is in mask coordinates (after padding) - that's why the node returns bbox and scale rather than pretending everything's free. Keep panel, mask, crop, and bbox in lockstep and the paste-back just works.

Installing

Part of comfyui-panels by bmad4ever. Install via ComfyUI Manager (search "comfyui-panels") and restart, or clone:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_panels

Deps are shapely, matplotlib, opencv-python - no model downloads, CPU-only math. Shapely is pinned exactly, so watch for version clashes with other packs.

CategoryBmad/Panels

Inputs (4)

NameTypeDefaultDescription
polygonPOLYGON
approx_resCOMBO10485763 options: 262144, 1048576, 1638400
padCOMBO644 options: 16, 32, 64, 128
imageoptIMAGEOptional image to be cropped with respect to the pologon's bounds, then resized and padded similarly to the mask. Mainly for editing workflows.

Outputs (4)

NameTypeDescription
maskMASKMask resized and padded.
bboxBBOXThe returned mask bounds ignoring the padding.
imageIMAGEIf an image is provided, returns a resized padded crop of the image; otherwise returns None.
scaleFLOATThe scale applied to the polygon in order to obtain the mask at the target resolution.