ComfyUI Node

Clamp

Keep your image math from exploding — the pack's most underrated safety node

By CoiiChan·Created about a year ago·Updated about a year ago· 2
Clamp
  • imga
  • IMAGE
formularesult = np.clip(imga, pfloat1, pfloat2)
pfloat10.00
pfloat21.00

ComfyUI's image tensors happily hold values outside 0–1. Add two bright images and you get 2.0. Divide by something dark and you get 200. The math is correct - and then it quietly poisons everything downstream, because samplers, previews, and most nodes assume they're looking at a normal image. Clamp is the guardrail: it squeezes every pixel back into the range you specify. result = np.clip(imga, pfloat1, pfloat2).

You will reach for this node every single time you do arithmetic on images with this pack, and if you don't, you'll wonder why your composite looks like someone turned the contrast to eleven. It's not glamorous; it's the difference between "the formula worked" and "the formula worked and the result is usable."

How it works

np.clip is elementwise and trivial - values below the minimum get set to the minimum, values above the maximum get set to the maximum. The two parameters are the clamp bounds:

  • pfloat1 - Clamp Min (0 by default).
  • pfloat2 - Clamp Max (1 by default).

Both are 0–1, which covers the usual case. Slightly annoying if you wanted to clamp to a different range (say 0–2 to preserve an HDR-ish intermediate), but for the 95% case - force everything back to a displayable image - the defaults are right.

Inputs and outputs

  • formula - result = np.clip(imga, pfloat1, pfloat2), pre-filled and editable.
  • imga - the image to clamp.
  • pfloat1 / pfloat2 - the low and high bounds.

One IMAGE output.

Install

ComfyUI Manager → search "FuncAsTexture", or:

cd ComfyUI/custom_nodes
git clone https://github.com/CoiiChan/ComfyUI-FuncAsTexture-CoiiNode
# restart ComfyUI

No models, no requirements.txt. Category: FunctionAsTexture.

Where it earns its keep

  • After any Add or multiply chain. If you're building blends out of the pack's arithmetic nodes, a Clamp at the end is the difference between a clean composite and blown-out whites.
  • Before previewing anything you've computed. Remember: the node preview clamps, but the tensor doesn't. Clamp makes reality match the preview.
  • As a poor man's contrast control. Setting min/max to 0.2/0.8 crushes the image into a narrower range - useful for turning a soft gradient into a harder one.

Notes

  • Clipping is destructive. Values outside the range are lost, not compressed. If you're building a multi-stage pipeline where an intermediate value matters (like a 0–2 range), clamp late, not early.
  • Clamp Min above Clamp Max produces a flat image at the min value. There's no validation stopping you; there's also no reason to do it.
  • NaN input stays NaN. Clamp fixes overflow, not the divide-by-zero NaNs from the pack's Divided node - if you clamp and still see garbage pixels, go fix the division, not the clamp.

Treat Clamp the way you treat torch.clamp in your own code: as the punctuation mark at the end of every math sentence. The pack ships a lot of math with sharp edges; this is the node that files them off.

CategoryFunctionAsTexture

Inputs (4)

NameTypeDefaultDescription
formulaSTRINGresult = np.clip(imga, pfloat1, pfloat2)
imgaoptIMAGEOptional ,Reference size ,Default =(1,512,512,3)
pfloat1optFLOAT0.000–1Optional,Clamp Min.
pfloat2optFLOAT1.000–1Optional,Clamp Max.

Outputs (1)

NameTypeDescription
IMAGEIMAGE