stouputils.data_science.data_processing.image.invert module#

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

Invert the colors of an image.

This function inverts the colors of the input image by subtracting each pixel value from the maximum possible value based on the image type.

Parameters:
  • image (NDArray[Any]) – Input image as a NumPy array.

  • ignore_dtype (bool) – Ignore the dtype check.

Returns:

Image with inverted colors.

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]], dtype=np.uint8)
>>> inverted = invert_image(image)
>>> inverted.tolist()
[[245, 235, 225], [215, 205, 195], [185, 175, 165]]
>>> # Test with floating point image
>>> float_img = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
>>> [round(float(x), 1) for x in invert_image(float_img).flatten()]
[0.9, 0.8, 0.7, 0.6]
>>> # Test with RGB image
>>> rgb = np.zeros((2, 2, 3), dtype=np.uint8)
>>> rgb[0, 0] = [255, 0, 0]  # Red pixel
>>> inverted_rgb = invert_image(rgb)
>>> inverted_rgb[0, 0].tolist()
[0, 255, 255]
>>> ## Test invalid inputs
>>> invert_image("not an image")
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array