Nodes/ComfyUI-Info-Prompt-Toolkit/Trim Image by Margin
ComfyUI Node

Trim Image by Margin

Crop the headroom off a generated image in one step

By kinorax·Created 5 months ago·Updated about a month ago· 2
Trim Image by Margin
  • image
  • margin
  • image

Trim Image by Margin crops an image by a margin bundle - top, right, bottom, left. It's the payout step of the pack's trim-margin workflow, and on its own it's almost boringly simple: give it an image and a margin, get the cropped image back.

But the why is worth understanding, because it's a genuinely good trick that people reinvent badly. The pack's Aspect Ratio to Size (Trim Margin) computes a sampling size that's larger than the final image you want. You sample that bigger size, then trim the edges down to your real target. Generating with headroom is a known quality move - diffusion models are weakest right at the image borders, and edges of the frame tend to come back mushy or artifacted. Sample a little big, crop the frame, and your final image has clean edges where the model's ugly border behavior used to be. This node is the crop.

How it works

  • image (required) - the image to trim.
  • margin (optional) - the IPT-Margin bundle (top/right/bottom/left) that says how much to cut from each edge.

Output is the trimmed image.

The key behavioral detail: if margin is unconnected, the image passes through unchanged. No crop, no error, no surprise. That makes it a safe node to leave in a workflow - it only trims when a margin actually shows up. So you can wire it permanently after your VAE decode, and it crops when the size-with-margin path feeds it a margin, and does nothing when it doesn't.

Where the margin comes from

In the intended setup it comes straight from Aspect Ratio to Size (Trim Margin), which is the node that produces the margin bundle alongside the sampling size. You don't type four numbers by hand - the size node and the trim node are two halves of one configuration: size-with-margins in, margin bundle out, crop applied here. Because the two are generated together, the margins always match the sampled size, and there's no way for the crop to drift from the canvas that was actually rendered.

The margins are also persisting-friendly: the pack's Set Margin Extra / Get Margin Extra pair stores the margin bundle in image metadata and restores it on reload. That's how you get a trim workflow that survives save/load - the crop configuration comes back with the image.

Install

Standard pack install:

cd ComfyUI/custom_nodes
git clone https://github.com/kinorax/comfyui-info-prompt-toolkit.git
cd comfyui-info-prompt-toolkit
pip install -r requirements.txt

Or ComfyUI Manager → search "ComfyUI-Info-Prompt-Toolkit" → install → restart. No models, no extra dependencies.

Where people get burned

The most common miss is thinking of this as a general crop tool. It isn't - it takes a margin bundle, not coordinates, and it expects that bundle to come from the companion size node. If you need an arbitrary crop of a region, that's a different node family entirely. And while the pass-through behavior is friendly, it can hide mistakes: if you meant to trim and the margin socket is accidentally unconnected, the node silently does nothing, and you get an untrimmed image with no error to tell you why. Wire the margin, then check the output once.

CategoryInfo-Prompt-Toolkit/ImageInfo

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
marginoptIPT-Margin

Outputs (1)

NameTypeDescription
imageIMAGE