ComfyUI Node

Auto Crop

Trimming white space so your object actually fills the frame — before it hits a generator or a measure

By mikemojen·Created 8 months ago·Updated 3 months ago· 0
Auto Crop
  • image
  • cropped_image
margin2
white_threshold250
alpha_threshold0.10

Auto Crop is the unglamorous but reliable node that removes white or transparent margins from an image and tightens the content to the frame edge. It's the kind of thing you could do by hand in an image editor - and probably would, once - until you realize you're doing it for the fiftieth time in the middle of a workflow that generates images in a loop. Then it becomes the node that saves you fifteen seconds of manual cropping per image.

How it works

It scans for the bounding box of "content" and crops to it, with a configurable margin added around the edges. The definition of background is where the three settings come in:

  • For RGB images, a pixel is background if all three channels are at or above white_threshold (0–255, default 250). It's checking all channels, so a pure-white page works but a warm cream background at 245/245/240 won't count as white at the default - you'd drop the threshold to catch it.
  • For RGBA images, background is transparent or white. Pixels with alpha below alpha_threshold (0–1, default 0.1) count as transparent, and white pixels count only if they're opaque. This is the setting to touch when you're cropping PNGs with alpha channels from an image-generation pipeline.

Then it finds the first and last non-background rows and columns, crops the image to that box, and pads by margin pixels (default 2) so the content doesn't sit hard against the edge. If there's genuinely no content - an all-white frame - it returns the image unchanged rather than erroring out, which is a small courtesy you'll appreciate.

One output: cropped_image.

Where it fits

The obvious chain: Load Image → Auto Crop → Extract Black → LineDetector for diagram measurement, where a big white margin does nothing but inflate coordinates and slow processing. It's also useful before image-to-image or outpainting workflows that care about the composition actually filling the canvas - a generated image with a thick white border is a classic source of "why is there a white frame in my output." And it composes well with the pack's ImageResizeKeepRatio-style nodes, if you're using them to standardize dimensions afterward.

Install

Part of ComfyUI-HappNodeSet (mikemojen / "Happrecision"). ComfyUI Manager: search HappNodeSet. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/mikemojen/ComfyUI-HappNodeSet.git
pip install -r ComfyUI-HappNodeSet/requirements.txt

Restart ComfyUI. Standard pack dependencies (numpy, OpenCV, scipy, scikit-image, Pillow, torch, svgwrite) - nothing that should break an existing install.

Common issues

The classic failure is a background that isn't white enough: JPEG artifacts and off-white fills leave a thin border of near-white that survives the default 250 threshold, so the crop looks like it "missed." Drop white_threshold to 240 or 245 for JPEGs. The flip side - white_threshold too low - eats light-gray content, so find the sweet spot for your source and stop touching it. For alpha-heavy PNGs, the alpha_threshold default of 0.1 means pixels at 5–10% opacity count as transparent, which is right for most edges but can shave soft shadows. And remember it crops per-frame in a batch: if your batch mixes images with and without borders, you'll get differently-sized outputs, so keep batches uniform or normalize afterward.

Categoryimage/transform

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
marginoptINT20–100
white_thresholdoptINT250200–255Pixels with all RGB values above this are considered white (0-255)
alpha_thresholdoptFLOAT0.100–1Pixels with alpha below this are considered transparent (0-1)

Outputs (1)

NameTypeDescription
cropped_imageIMAGE