Compact integer arrays
Store nonnegative values in a compact, byte-sized representation.
NumPy · Type Alias
np.uint8 is NumPy’s unsigned 8-bit integer type. It represents nonnegative integer data and is commonly used as an array dtype for byte-sized values.
np.uint8 is NumPy’s unsigned integer scalar type with 8 bits and no sign. Use it as an array dtype or as a scalar constructor when values fit its representable range; NumPy identifies arrays using this dtype as uint8.
numpy.uint8(value)A NumPy unsigned 8-bit integer scalar when called as np.uint8(value); when supplied as an array dtype, it specifies the array’s data type.
np.uint8 stores unsigned integer values in 8 bits.numpy.uint8; its C-like alias is numpy.ubyte, corresponding to an unsigned char.np.uint8 describes a NumPy data type with defined characteristics..astype(np.uint8)..dtype attribute to confirm whether it uses uint8.import numpy as np
pixels = np.array([0, 128, 255], dtype=np.uint8)
print(pixels.dtype)
print(np.iinfo(np.uint8).min, np.iinfo(np.uint8).max)Creates an array with the unsigned 8-bit integer data type and displays its dtype and representable limits.
Store nonnegative values in a compact, byte-sized representation.
Represent image channels, masks, or other byte-oriented data.
Match interfaces that require an unsigned 8-bit integer.
np.uint8 represent?np.uint8 is an unsigned integer type stored using 8 bits. Its values have no sign, so the representable range is 0 through 255.
Follow-up: How would you verify the range programmatically? Use np.iinfo(np.uint8).min and .max rather than hard-coding limits.
np.uint8 commonly used?Specify it as an array dtype, for example np.array([1, 2], dtype=np.uint8), or construct a NumPy scalar with np.uint8(1). The resulting array reports dtype('uint8').
Follow-up: How can an existing array be converted? Use its .astype(np.uint8) method, while ensuring the values are appropriate for the target type.
Its 8-bit unsigned representation is useful when storage or interoperability requires byte-sized nonnegative integers. Arithmetic still follows NumPy dtype-promotion rules, so combining it with other types can change the result dtype.
Follow-up: What should be checked when mixing it with Python integers? Check the resulting dtype and possible overflow, because promotion and overflow behavior depend on the operands.
numpy.ubytenumpy.dtypenumpy.iinfondarray.astypenumpy.uint16numpy.int8