ComfyUI Node

Pt To Uint8

Casting to image bytes — mind the wrapping

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt To Uint8
  • tens_a
  • TENSOR

Pt To Uint8 is the cast you reach for when you want a tensor to look like actual image byte data. uint8 is the 0-to-255 unsigned integer dtype - the one PNGs, JPEGs, and virtually every image pipeline think in. In ComfyUI-Pt-Wrapper, this node is how you take a float tensor that came out of some math or a model and turn it into something an image-oriented node can digest.

One required input (tens_a, TENSOR), one output (TENSOR), one-liner internals: tens_a.to(torch.uint8). Same family as PtToInt16/32/64, but with a personality of its own - because uint8's semantics around the edges are genuinely weird and worth knowing before you rely on it.

The trap: casting floats to uint8 doesn't clip to 0–255, it wraps. Values below 0 and above 255 come back around modulo 256. So 300 becomes 44, and −1 becomes 255. If you cast a tensor that has stray values outside the valid range, you won't get a warning - you'll get silently scrambled bytes. The fix is to clamp (or scale) before you cast. And the usual truncation rule applies on top: a normalized 0–1 image tensor cast straight to uint8 is all zeros. Scale to 0–255 first.

That said, when your values are already in range, uint8 is exactly right, and it's the natural endpoint if you're building image data from scratch inside the Pt graph - cast, then hand it to PtToImage and out to ComfyUI's own preview/save nodes.

Install

ComfyUI Manager (search "ComfyUI-Pt-Wrapper"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper

Restart ComfyUI. The dependency tree is the heavy one: transformers, datasets, peft, accelerate, scikit-learn, pandas, seaborn, matplotlib, gensim, sentencepiece. No model files required.

Common issues

If your "image" comes out as black or garbled after a uint8 cast, nine times out of ten it's the range, not the node. Check what your tensor's min/max actually are before the cast - a debug/print node in the pack will tell you. Clamp to [0, 255], cast, and it'll behave. And if you're coming from ComfyUI's standard IMAGE format, remember those are float 0–1 tensors - you almost certainly want to scale, not just cast.

CategoryData Analysis

Inputs (1)

NameTypeDefaultDescription
tens_aTENSOR

Outputs (1)

NameTypeDescription
TENSORTENSOR