stouputils.data_science.data_processing.image.translation module#

translate_image(image: ndarray[Any, dtype[Any]], x: float, y: float, padding: int = 0, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Translate an image

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

  • x (float) – Translation along the x axis (between -1 and 1)

  • y (float) – Translation along the y axis (between -1 and 1)

  • padding (int) – Padding that has been added to the image before calling this function

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Translated image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.uint8)
>>> translate_image(image, 0.5, 0.5).tolist()
[[0, 0, 0], [0, 1, 2], [0, 4, 5]]
>>> translate_image(image, 0, -2/3).tolist()
[[7, 8, 9], [0, 0, 0], [0, 0, 0]]
>>> ## Test invalid inputs
>>> translate_image(image, 2, 0)
Traceback (most recent call last):
        ...
AssertionError: x must be between -1 and 1, got 2
>>> translate_image(image, 0, 2)
Traceback (most recent call last):
        ...
AssertionError: y must be between -1 and 1, got 2
>>> translate_image("not an image", 0, 0)
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> translate_image(image, 0, 0, padding=-1)
Traceback (most recent call last):
        ...
AssertionError: padding must be positive, got -1