Image.astype#

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

Cast image datatype

Parameters:

dtype (str or NumPy dtype) – Numpy data type

The short-name aliases 'int', 'float', 'double', 'half' are also accepted and resolve to the Toolbox’s own defaults (uint8, float32, float64, float16 respectively) – this matters because NumPy’s own np.dtype('float') resolves to float64, not float32. See DTYPE_ALIASES.

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.

Examples

>>> from machinevisiontoolbox import Image
>>> img = Image.Random(size=3)
>>> img.print()
   206 114 226
   192 254 247
    30   3 134
>>> img.astype('float').print()
   206.00 114.00 226.00
   192.00 254.00 247.00
    30.00   3.00 134.00
>>> img = Image.Random(size=3, dtype='float')
>>> img.print()
   0.74 0.07 0.60
   0.35 0.17 0.57
   0.31 0.58 0.13
>>> img.astype('uint8').print()
   0 0 0
   0 0 0
   0 0 0

See also

to