tf.keras.ops.image.scale_and_translate

Apply a scale and translation to the images.

Generates a new image of output_shape by resampling from the input image using the sampling method corresponding to method. For 2D images, this operation transforms a location in the input images, (x, y), to a location in the output image according to:

(x * scale[1] + translation[1], y * scale[0] + translation[0]).

(Note the inverse warp is used to generate the sample locations.) Assumes half-centered pixels, i.e the pixel at integer location row, col has coordinates y, x = row + 0.5, col + 0.5, and similarly for other input image dimensions.

If an output location(pixel) maps to an input sample location that is outside the input boundaries then the value for the output location will be set to zero.

The method argument expects one of the following resize methods:

  • "linear", "bilinear", "trilinear", "triangle": Linear interpolation. If antialias is True, uses a triangular filter when downsampling.
  • "cubic", "bicubic", "tricubic": Cubic interpolation, using the Keys cubic kernel.
  • "lanczos3": Lanczos resampling, using a kernel of radius 3.
  • "lanczos5": Lanczos resampling, using a kernel of radius 5.

images The input array.
output_shape The output shape, as a sequence of integers with length equal to the number of dimensions of image.
scale A [K] array with the same number of dimensions as images, containing the scale to apply in each dimension.
translation A [K] array with the same number of dimensions as images, containing the translation to apply in each dimension.
spatial_dims A length K tuple specifying the spatial dimensions that the passed scale and translation should be applied to.
method A string specifying the resizing method to use. Available methods are "linear", "bilinear", "trilinear", "triangle", "cubic", "bicubic", "tricubic", "lanczos3" and "lanczos5".
antialias Whether an antialiasing filter should be applied when downsampling. Has no effect when upsampling. Defaults to True.

The scale and translated images.

Example:

images = np.arange(9, dtype="float32").reshape((3, 3))
scale = np.array([2.0, 2.0]).astype("float32")
translation = -(scale / 2.0 - 0.5)
resized_images = keras.image.scale_and_translate(
    images, (5, 5), scale, translation, (0, 1), "linear"
)
resized_images
array([[0.0 0.5 1.0 1.5 2.0]
       [1.5 2.0 2.5 3.0 3.5]
       [3.0 3.5 4.0 4.5 5.0]
       [4.5 5.0 5.5 6.0 6.5]
       [6.0 6.5 7.0 7.5 8.0]], dtype=float32)