machinevisiontoolbox.Image.to

Image.to(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([[188,   5, 105],
       [136,  65,  12],
       [ 23,  22, 180]], dtype=uint8)
>>> img.to('float').image
array([[0.7373, 0.0196, 0.4118],
       [0.5333, 0.2549, 0.0471],
       [0.0902, 0.0863, 0.7059]])
>>> img = Image.Random(3, dtype='float')
>>> img.image
array([[0.5256, 0.1569, 0.8595],
       [0.8463, 0.7323, 0.9068],
       [0.3467, 0.8664, 0.2802]])
>>> img.to('uint8').image
array([[134,  40, 219],
       [216, 187, 231],
       [ 88, 221,  71]], dtype=uint8)
Seealso:

astype to_int to_float