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,float16respectively) – this matters because NumPy’s ownnp.dtype('float')resolves tofloat64, notfloat32. SeeDTYPE_ALIASES.- Returns:
image
- Return type:
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() 88 147 245 195 58 85 68 104 138 >>> img.astype('float').print() 88.00 147.00 245.00 195.00 58.00 85.00 68.00 104.00 138.00 >>> img = Image.Random(size=3, dtype='float') >>> img.print() 0.04 0.96 0.82 0.93 0.38 0.23 0.30 0.24 0.25 >>> img.astype('uint8').print() 0 0 0 0 0 0 0 0 0
- Seealso: