Image.to#

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

Convert 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. 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(size=3)
>>> img.print()
   199 172 127
   252 216 163
    60  20  61
>>> img.to('float').print()
   0.78 0.67 0.50
   0.99 0.85 0.64
   0.24 0.08 0.24
>>> img = Image.Random(size=3, dtype='float')
>>> img.print()
   0.43 0.24 0.33
   0.77 0.20 0.63
   0.81 0.10 0.81
>>> img.to('uint8').print()
   109  62  84
   197  52 162
   207  26 206
Seealso:

astype to_int to_float