Nodes/ComfyUI-aihub-workflow-exposer/AIHub Utils Fit Layer To Merged Image
ComfyUI Node

AIHub Utils Fit Layer To Merged Image

Trim a layer down to the canvas — the reverse of the merged-image crop

By otavanopisto·Created about a year ago·Updated 22 days ago· 7
AIHub Utils Fit Layer To Merged Image
  • layer_image
  • layer_mask
  • IMAGE
  • MASK
  • POS_X
  • POS_Y
  • WIDTH
  • HEIGHT
merged_image_width1024
merged_image_height1024
layer_pos_x0
layer_pos_y0

AIHubUtilsCropMergedImageToLayerSize crops a merged image down to a layer's size. AIHubUtilsFitLayerToMergedImage is the mirror image: it takes a layer image and trims it down to the canvas. Same geometry problem, opposite input. In the otavanopisto ComfyUI-aihub-workflow-exposer layer-workflow world, this is the fallback for clients that can't provide the current_layer_at_image_intersection expose type - the one that hands you the current layer already cut to the visible canvas area.

Here's the situation. An editor's current layer can be bigger than the canvas and sitting at arbitrary coordinates. When a client can't pre-intersect it, you get the full layer image plus its position. That layer is full of pixels that aren't visible in the canvas - the README calls that "pointless data since it will be invisible." This node fixes it: it intersects the layer's bounding box with the merged image's bounds and returns the visible portion, plus the new position and size so everything downstream stays aligned.

The inputs that matter

  • layer_image - the full layer pixels (and layer_mask, optional, cropped to match).
  • layer_pos_x / layer_pos_y - the layer's position in the canvas (default 0, range ±10000).
  • merged_image_width / merged_image_height - the canvas size (default 1024×1024).

The six outputs are the payoff: IMAGE, MASK, POS_X, POS_Y, WIDTH, HEIGHT. Those last four are exactly what you need to feed region-based inpaint or compositing nodes - the layer trimmed to the canvas, positioned correctly relative to it. The README's pitch: this is "equivalent to if current_layer_at_image_intersection was given but from the current layer."

How it behaves

Pure bounding-box math: intersect the two rectangles, crop the layer image at the intersection, and emit the intersection's coordinates and size. The one sharp edge: if the layer is completely outside the canvas, the source raises ValueError: The layer is completely outside the bounds of the merged image. A layer that only partially overlaps is fine - you just get the visible sliver. Partially-off-canvas layers are the normal case here, not the error case.

When you actually need it

Same rule of thumb as its sibling: use it only when your client can't deliver the pre-intersected expose type. If the client can hand you current_layer_at_image_intersection directly, wire that and drop this node. If not, this is the piece that keeps your region logic honest - without it, you'd be sending the sampler a layer image whose coordinates don't match its content.

Install

No requirements, like the rest of the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/otavanopisto/ComfyUI-aihub-workflow-exposer

Restart ComfyUI. Honest note: the geometry here is simple enough that you could rebuild it with a crop node and some math - but this node bundles the position/size outputs in one go, which is the part people usually get wrong when they hand-roll it.

Categoryaihub/utils

Inputs (6)

NameTypeDefaultDescription
merged_image_widthINT1024The width of the merged image
merged_image_heightINT1024The height of the merged image
layer_pos_xINT0-10000–10000The X position of the layer in the canvas
layer_pos_yINT0-10000–10000The Y position of the layer in the canvas
layer_imageIMAGEThe image of the layer
layer_maskoptMASKThe mask of the layer

Outputs (6)

NameTypeDescription
IMAGEIMAGE
MASKMASK
POS_XINT
POS_YINT
WIDTHINT
HEIGHTINT