Image.array_as#

Image.array_as(dtype: DTypeLike | None = None) ndarray[source][source]#

Convert Image to NumPy array of specified type

Parameters:

dtype (str or np.dtype) – data type of the output array

Returns:

NumPy array with specified type

Return type:

ndarray(H,W) or ndarray(H,W,P)

Return a NumPy array with pixels converted to the specified data type.

Input

Output int

Output float

float

scaled [-1,1] → [min_int, max_int]

cast, values unchanged

int

scaled by max_int_output/max_int_input

scaled [min_int, max_int] → [-1, 1]

uint

scaled by max_int_output/max_int_input

scaled [0, max_int] → [0, 1]

bool

False → 0, True → max_int

False → 0.0, True → 1.0

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image([[5_000, 10_000], [30_000, 60_000]]) # uint16 image
>>> img
Image(size=(2, 2), dtype=uint16)
>>> img.array_as('uint8')
array([[ 19,  38],
       [116, 233]], dtype=uint8)
>>> img.array_as('uint32')
array([[ 327685000,  655370000],
       [1966110000, 3932220000]], dtype=uint32)
>>> img.array_as('float')
array([[0.0763, 0.1526],
       [0.4578, 0.9155]])
>>> img = Image([[0.0, 0.3], [0.5, 1.0]])
>>> img
Image(size=(2, 2), dtype=float32)
>>> img.array_as('uint8')
array([[  0,  77],
       [128, 255]], dtype=uint8)
>>> img = Image([[False, True], [True, False]])
>>> img
Image(size=(2, 2), dtype=bool)
>>> img.array_int('uint8')
!! [RUNBLOCK-ERROR] machinevisiontoolbox/ImageCore.py:25
    AttributeError: 'Image' object has no attribute 'array_int'. Did you mean: 'array_as'?
>>> img.array_int('float')
!! [RUNBLOCK-ERROR] machinevisiontoolbox/ImageCore.py:25
    AttributeError: 'Image' object has no attribute 'array_int'. Did you mean: 'array_as'?

Note

Works for greyscale or color (arbitrary number of planes) image

Warning

This method assumes that all integer values are unsigned.

Seealso:

array array_float cast like