machinevisiontoolbox.Image.to

Image.to(dtype: DType)[source]

Convert 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. Integer values are scaled according to the maximum value of the datatype, floating values are in the range 0.0 to 1.0.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Random(3)
>>> img.image
array([[150, 245,   6],
       [130, 201,  88],
       [101,  32, 183]], dtype=uint8)
>>> img.to('float').image
array([[0.5882, 0.9608, 0.0235],
       [0.5098, 0.7882, 0.3451],
       [0.3961, 0.1255, 0.7176]])
>>> img = Image.Random(3, dtype='float')
>>> img.image
array([[0.28  , 0.2974, 0.1698],
       [0.0128, 0.4187, 0.4562],
       [0.6005, 0.6138, 0.3202]])
>>> img.to('uint8').image
array([[ 71,  76,  43],
       [  3, 107, 116],
       [153, 157,  82]], dtype=uint8)
Seealso:

astype to_int to_float