liveBook · Manning

Unsigned integer

As explained in Fast Python: High performance techniques for large datasets

An unsigned integer is an integer type or representation whose values are nonnegative, with its representable range determined by its bit width and applicable rules.

An 8-bit unsigned integer represents grayscale pixel values from 0 through 255. In NumPy, arithmetic that exceeds this range wraps: 255 + 5 becomes 4, while doubling 255 produces 254. Images can look plausible despite these incorrect values. section 4.2.3

brighter3_arr = image_arr.astype(np.uint16)
brighter3_arr = brighter3_arr * 2
print(brighter3_arr.max(), brighter3_arr.dtype)
brighter3_arr = np.minimum(brighter3_arr, 255)   #1
print(brighter3_arr.max(), brighter3_arr.dtype)
brighter3_arr = brighter3_arr.astype(np.uint8)
print(brighter3_arr.max(), brighter3_arr.dtype)
Image.fromarray(brighter3_arr).save("brighter3.png")

Widen the array to uint16 before doubling, clamp results to 255, then cast back to uint8 for the image. section 4.2.3

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Demonstrates 8-bit overflow, then widens, clamps, and casts back; does not establish general integer-type rules.
Examples
Tests adding 5 to and doubling grayscale pixels, including 255 wrapping to 4 and doubling to 254.
What you can do after
Diagnose/debug with it: detect pixel-value wraparound, widen to uint16, clamp, and cast back to uint8.
How it connects
Connects unsigned integers to NumPy arrays, integer overflow, and data-type conversion.

Also explained in

Compare how 9 books explain unsigned integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.