machinevisiontoolbox.Image.astype

Image.astype(dtype)[source]

Cast image datatype

Parameters:

dtype (str) – Numpy data type

Returns:

image

Return type:

Image

Create a new image, same size as input image, with pixels of a different datatype. Values are retained, only the datatype is changed.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Random(3)
>>> img.image
array([[ 90,  49,  93],
       [132, 182, 159],
       [ 46, 144, 253]], dtype=uint8)
>>> img.astype('float').image
array([[ 90.,  49.,  93.],
       [132., 182., 159.],
       [ 46., 144., 253.]])
>>> img = Image.Random(3, dtype='float')
>>> img.image
array([[0.3788, 0.9709, 0.6438],
       [0.4097, 0.6287, 0.4167],
       [0.5385, 0.8559, 0.5004]])
>>> img.astype('uint8').image
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]], dtype=uint8)
Seealso:

to