np.uint8 represents integers from 0 through 255, inclusive. It is an unsigned, fixed-width 8-bit type. Values outside that range cannot be represented without changing them: current NumPy may raise OverflowError when constructing an array from an out-of-range Python integer, while casting an existing NumPy array can follow different rules. To preserve values, check the bounds before converting and use NumPy’s same_value casting option where your installed version supports it.
What is the range of np.uint8?
np.uint8 (also written numpy.uint8) is an unsigned integer type with 8 bits. Because it has no sign bit, its 256 possible bit patterns represent the inclusive range 0–255. Negative values and values greater than 255 are out of range.
To inspect the limits in Python, use np.iinfo:
import numpy as np
info = np.iinfo(np.uint8)
print(info.min, info.max) # 0 255
NumPy’s data types guide identifies uint8 as an unsigned 8-bit type and documents numpy.iinfo for inspecting integer limits. Prefer the explicitly sized uint8 name when you need a fixed width; some C-like integer aliases can depend on the platform.
What happens when converting a negative number to uint8?
The value is outside the type’s range, but the outcome depends on the conversion route. Do not assume that every conversion either wraps or raises an error.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
Constructing an array from Python integers
Python’s built-in integers have flexible precision; NumPy integer types have fixed precision. Current NumPy’s array-creation documentation shows that constructing an integer array from a Python integer that does not fit the requested dtype can raise OverflowError. The guide’s specific example uses int8; for uint8, the corresponding range to check is 0–255. Do not rely on an expression such as np.array([-1], dtype=np.uint8) as a portable way to request wraparound.
Casting an existing NumPy array
A cast from an existing NumPy value is a different operation. NumPy’s dtype guide says casts follow C casting rules and can overflow. Its example casts 300 from int64 to int8, producing 44 because 300 − 256 = 44. That example demonstrates a cast between existing NumPy values; it does not establish that every constructor or conversion API wraps in the same way.
Rank #2
When a conversion must preserve values, NumPy documents astype(..., casting="same_value"), which raises if casting would change a value. Check the documentation for your installed NumPy version: the current stable manual may describe options that older releases do not provide.
How do I convert to uint8 without overflow?
Check that every input is within the inclusive range 0–255 before converting. Then use same_value as an additional guard when available:
Recommended Free Tools
import numpy as np
info = np.iinfo(np.uint8)
if np.any((values < info.min) | (values > info.max)):
raise ValueError("values outside uint8 range")
result = np.asarray(values).astype(np.uint8, casting="same_value")
The bounds check makes the input requirement explicit; the cast option checks that conversion does not alter values. If inputs should remain arbitrary-precision integers, keep them as Python int values or use a wider representation instead of forcing them into uint8.
Can uint8 arithmetic overflow?
Yes. Fixed-width arithmetic can exceed the dtype’s representable range. NumPy’s data type promotion guide describes current behavior: scalar overflow warns, but array overflow may not. For example, the guide notes that np.array(100, dtype=np.uint8) + 100 does not warn. A missing warning is not proof that a calculation stayed in range.
For calculations whose results may exceed 255, use a dtype that can represent the intermediate result before performing the operation, or validate the inputs and results against the bounds you require. Do not use warnings as your range check.
Python integers and NumPy promotion
Since NumPy 2.0, promotion with Python scalar values considers the scalar’s kind but ignores its precision when choosing the result dtype. A Python integer combined with a low-precision NumPy integer therefore does not necessarily widen the operation. An out-of-range Python integer can also fail during coercion for a NumPy scalar operation. These rules are version-sensitive; verify behavior if your code must support NumPy releases before 2.0.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteBest Value
numpy.can_cast is a dtype-level check, not a check that a particular value fits. Since NumPy 2.0 it does not accept Python scalars, and it does not apply value-based range checks to 0-D arrays or NumPy scalars. Use actual bounds checks for individual values rather than treating can_cast as proof that a conversion is safe; see the official numpy.can_cast reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




