Skip to main content

Fixed: 255 + 50 Gives 49 for a uint8 Tensor in TensorFlow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Brightening pixels by adding 50 to a uint8 tensor gave [ 50 178 24 49]: the two brightest pixels came out nearly black, with no error. A uint8 holds only 0 to 255, and a sum past 255 wraps around to 0. Adding in a bigger type and clipping the result to 0–255 fixed it.

What I Was Doing​

I was brightening four pixel values by adding 50 to each. The pixels were uint8, the type a decoded photo has, with one black pixel, 0, and one white, 255:

brighten.py
import tensorflow as tf

pixels = tf.constant([0, 128, 230, 255], dtype=tf.uint8)
print(pixels + 50)

I ran it in a uv project with TensorFlow 2.22.0:

Terminal
uv run python brighten.py

What I Expected​

Every pixel 50 brighter: 50, 178, and the two bright ones at the most a pixel can be, 255.

What I Got​

Output
tf.Tensor([ 50 178 24 49], shape=(4,), dtype=uint8)

The terminal after uv run python brighten.py: the uint8 tensor 50, 178, 24 and 49.

The first two are right. But 230 became 24 and 255 became 49: the brightest pixels turned almost black. There was no error or warning.

What I Checked​

1. The wrong numbers​

230 + 50 is 280, and the result was 24: 280 − 256. 255 + 50 is 305, and the result was 49: 305 − 256. Both sums went past 255 and started counting again from 0.

2. What uint8 can hold​

tf.uint8.min and tf.uint8.max printed 0 and 255. The tf.dtypes.DType page describes max as "the maximum representable value in this data type". A uint8 has room for 256 values, 0 to 255, and no more.

3. What type the result has​

(pixels + 50).dtype is uint8 too. The tf.math.add API page explains, in a warning: "If one of the inputs (x or y) is a tensor and the other is a non-tensor, the non-tensor input will adopt (or get casted to) the data type of the tensor input. This can potentially cause unwanted overflow or underflow conversion." 50 became a uint8, the sum was computed as uint8, and 280 and 305 didn't fit.

Why It Happens​

Adding a number to a uint8 tensor gives a uint8 result, and a uint8 can't hold anything above 255. A sum that goes past 255 wraps around: it loses 256 and starts again from 0, so 280 became 24.

The Fix​

Add in a type with room for bigger numbers, clip the result to 0–255, and convert it back to uint8:

brighten.py
import tensorflow as tf

pixels = tf.constant([0, 128, 230, 255], dtype=tf.uint8)

brighter = tf.clip_by_value(tf.cast(pixels, tf.int32) + 50, 0, 255)
print(tf.cast(brighter, tf.uint8))

tf.cast(pixels, tf.int32) makes an int32 copy, which holds 280 and 305 without trouble. tf.clip_by_value(…, 0, 255) then limits every value to that range. Its API page says: "Any values less than clip_value_min are set to clip_value_min. Any values greater than clip_value_max are set to clip_value_max." Every value now fits in uint8, so the last cast is safe.

Another way: tf.dtypes.saturate_cast​

tf.dtypes.saturate_cast(tf.cast(pixels, tf.int32) + 50, tf.uint8) clips and converts in one step, and returned the same [ 50 178 255 255]. Its API page says: "This function casts the input to dtype without overflow. If there is a danger that values would over or underflow in the cast, this op applies the appropriate clamping before the cast."

Another way: tf.image.adjust_brightness​

For images, tf.image.adjust_brightness(pixels, 50 / 255) also returned [ 50 178 255 255]. It works on brightness from 0 to 1, so 50 out of 255 is 50 / 255. TensorFlow's source describes it as "a convenience method that converts RGB images to float representation, adjusts their brightness, and then converts them back to the original data type."

After the Fix​

Terminal
uv run python brighten.py
Output
tf.Tensor([ 50 178 255 255], shape=(4,), dtype=uint8)

The terminal after the fix: the uint8 tensor 50, 178, 255 and 255.

0 and 128 went up by 50, and 230 and 255 stopped at 255, white. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Adding in int32 but casting back without clippingtf.cast(tf.cast(pixels, tf.int32) + 50, tf.uint8) returned [ 50 178 24 49] again: the cast wrapped the big valuesClip to 0–255 before the cast
Adding a number above 255pixels + 300 returned [ 44 172 18 43]: 300 itself became 44 (300 − 256) before the additionCast the pixels to int32 first
Darkening the same waypixels - 50 returned [206 78 180 205]: 0 - 50 wrapped to 206Subtract in int32 and clip: the result was [ 0 78 180 205]

Troubleshooting​

SymptomLikely causeFix
Bright pixels turn dark after brighteningA uint8 sum went past 255 and wrappedAdd in int32, clip to 0–255, cast back
Dark pixels turn bright after darkeningA uint8 difference went below 0 and wrappedSubtract in int32, clip, cast back
Values over 255 in a float32 imagefloat32 doesn't wrap: tf.cast(pixels, tf.float32) + 50 gave [ 50. 178. 280. 305.]Clip before converting back to uint8

FAQ​

Does this happen with decimals?​

Not the wrap-around. In float32, the same sum gave 280. and 305.. Those are still too big for a pixel, so clip them before converting back to uint8.

Why 256 and not 255?​

A uint8 has 256 possible values, 0 to 255. Counting past the last one starts again at the first, so every wrapped result is 256 less than the true sum.

Conclusion​

[ 50 178 24 49] instead of [ 50 178 255 255] meant the uint8 sums 280 and 305 wrapped around past 255. tf.clip_by_value(tf.cast(pixels, tf.int32) + 50, 0, 255), cast back to uint8, kept the bright pixels at 255.

Additional Resources​