stouputils.data_science.data_processing.image.contrast module#

contrast_image(image: ndarray[Any, dtype[Any]], factor: float, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Adjust the contrast of an image.

Parameters:
  • image (NDArray[Any]) – Image to adjust contrast

  • factor (float) – Contrast adjustment factor

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Image with adjusted contrast

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> contrasted = contrast_image(image.astype(np.uint8), 1.5)
>>> contrasted.shape == image.shape
True
>>> img = np.array([[50, 100, 150]], dtype=np.uint8)
>>> high = contrast_image(img, 2.0)
>>> low = contrast_image(img, 0.5)
>>> bool(high.std() > img.std() > low.std())  # Higher contrast = higher std
True
>>> rgb = np.full((3,3,3), 128, dtype=np.uint8)
>>> rgb[1,1] = [50, 100, 150]
>>> cont_rgb = contrast_image(rgb, 1.5)
>>> cont_rgb.shape == (3,3,3)
True
>>> ## Test invalid inputs
>>> contrast_image("not an image", 1.5)
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> contrast_image(image.astype(np.uint8), "1.5")
Traceback (most recent call last):
        ...
AssertionError: factor must be a number, got <class 'str'>