Image.astype#

Image.astype(dtype: DTypeLike) Image[source][source]#

Cast image datatype

Parameters:

dtype (str or NumPy dtype) – 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(size=3)
>>> img.print()
    97  55  49
    37 129  29
   129 231  75
>>> img.astype('float').print()
    97.00  55.00  49.00
    37.00 129.00  29.00
   129.00 231.00  75.00
>>> img = Image.Random(size=3, dtype='float')
>>> img.print()
   0.87 1.00 0.72
   0.80 0.79 0.91
   0.47 0.25 0.66
>>> img.astype('uint8').print()
   0 0 0
   0 0 0
   0 0 0
Seealso:

to