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([[ 22,  63,  28],
       [ 61, 212,  67],
       [100, 131,  41]], dtype=uint8)
>>> img.to('float').image
array([[0.0863, 0.2471, 0.1098],
       [0.2392, 0.8314, 0.2627],
       [0.3922, 0.5137, 0.1608]])
>>> img = Image.Random(3, dtype='float')
>>> img.image
array([[0.3676, 0.7532, 0.7722],
       [0.5328, 0.0875, 0.3846],
       [0.6179, 0.9745, 0.1576]])
>>> img.to('uint8').image
array([[ 94, 192, 197],
       [136,  22,  98],
       [158, 249,  40]], dtype=uint8)
Seealso:

astype to_int to_float