Nodes/ComfyUI-ImageAlphaCrop/Image Alpha Crop (Advanced)
ComfyUI Node

Image Alpha Crop (Advanced)

Image Alpha Crop (Advanced) — trim, resize, and reuse the crop box

By swan7-py·Created 10 months ago·Updated 10 months ago· 4
Image Alpha Crop (Advanced)
  • transparent_images
  • original_images
  • cropped_images
padding0
alpha_threshold0.01
target_width0
target_height0
keep_aspect_ratiotrue
background_colortransparent

If you've cut a logo or product out of a photo, you know the result arrives wrapped in a big invisible frame of nothing. The basic Image Alpha Crop node in this pack trims that frame. The Advanced version does the trim and fixes the output to whatever size you need - and, in its most useful party trick, applies the crop to a different image than the one it read the transparency from.

That last bit is the sleeper feature. ComfyUI's pipeline is RGB-centric: alpha usually travels as a separate MASK tensor, and plenty of nodes quietly flatten a 4-channel image to 3. So the author's idea here is clean: feed one image that defines where the content is (the transparent one), and another image that is what you actually want cropped (original_images). If you leave original_images empty, it just crops the transparent image itself.

How it works

Mechanically it's the same bounding-box math as the basic node: pixels whose alpha clears alpha_threshold (default 0.01, i.e. ~2.5/255) count as visible, and the crop box spans the outermost visible pixel on each side. Because it takes the bounding box of all visible pixels, a logo with a hollow middle still gets a correct, complete crop - the box just spans the hole.

Then the part the basic node doesn't do:

  • If your source image isn't the same resolution as the transparent image, it's LANCZOS-resized to match first, so the crop box lines up.
  • If both target_width and target_height are greater than 0, the result is resized. With keep_aspect_ratio set to true it fits inside your target box, preserving proportions, and centers the result on a canvas of exactly target_width × target_height, padding the leftover space with background_color (transparent, black, or white). Set it to false and it just stretches to the target.

The inputs that matter

  • transparent_images - the image whose alpha determines the crop region (required).
  • original_images - optional; the actual pixels to crop, using that region.
  • target_width / target_height - both default to 0 (no resize). Note the catch: the resize only fires when both are set. Setting one and leaving the other at 0 does nothing.
  • keep_aspect_ratio, background_color - fit-and-letterbox vs. hard stretch, and what the letterbox is filled with.
  • padding, alpha_threshold - same as the basic node.

Worth knowing: the target dimensions step in increments of 8 (0–4096). That's not a bug, it's friendly to dataset prep - training tiles come in multiples of 8.

Where it earns its keep

The "transparent image sets the box, original image gets cropped" mode is the one you'll actually reach for in a pipeline. Example: you generate a transparent composite to establish the composition, but you want to crop your high-res original render to the same frame. Wire both in, out comes the original cropped to exactly what the transparency says is the content. The other big use case is uniform tiles: crop a batch of cutout PNGs, then set target_width/target_height to your training size with background_color set to transparent (or white, if your trainer doesn't like holes) and every output is identically sized.

Install

Same pack as the rest - no models, no pip dependencies, just numpy/torch/Pillow that ComfyUI already ships:

cd ComfyUI/custom_nodes
git clone https://github.com/swan7-py/ComfyUI-ImageAlphaCrop

then restart ComfyUI. Or search "ImageAlphaCrop" in ComfyUI Manager - the pack is published to the Comfy Registry, so it installs in one click.

Gotchas

One real trap: like the basic node, this one concatenates its per-image results into a batch at the end, so every image in a batch has to end up the same size. Different logos crop to different boxes, and torch.cat will throw a tensor-shape error. Feed one image at a time, or make sure your inputs genuinely crop identically. And a fully transparent input returns the source image unchanged rather than erroring - that's intentional, not a bug.

Output is a single cropped_images RGBA tensor. To keep the transparency intact on disk, save it with the pack's own Save Image (RGBA) node - a stock save node will happily flatten it back to RGB.

CategorySwan

Inputs (8)

NameTypeDefaultDescription
transparent_imagesIMAGE
paddingINT00–100
alpha_thresholdFLOAT0.010–1
target_widthINT00–4096
target_heightINT00–4096
keep_aspect_ratioCOMBOtrue2 options: true, false
background_colorCOMBOtransparent3 options: transparent, black, white
original_imagesoptIMAGE

Outputs (1)

NameTypeDescription
cropped_imagesIMAGE