tf.image.resize

Resize images to size using the specified method.

Used in the notebooks

Used in the guide Used in the tutorials

Resized images will be distorted if their original aspect ratio is not the same as size. To avoid distortions see tf.image.resize_with_pad.

image = tf.constant([
 [1,0,0,0,0],
 [0,1,0,0,0],
 [0,0,1,0,0],
 [0,0,0,1,0],
 [0,0,0,0,1],
])
# Add "batch" and "channels" dimensions
image = image[tf.newaxis, ..., tf.newaxis]
image.shape.as_list()  # [batch, height, width, channels]
[1, 5, 5, 1]
tf.image.resize(image, [3,5])[0,...,0].numpy()
array([[0.6666667, 0.3333333, 0.       , 0.       , 0.       ],
       [0.       , 0.       , 1.       , 0.       , 0.       ],
       [0.       , 0.       , 0.       , 0.3333335, 0.6666665]],
      dtype=float32)

It works equally well with a single image instead of a batch of images:

tf.image.resize(image[0], [3,5]).shape.as_list()
[3, 5, 1]

When antialias is true, the sampling filter will anti-alias the input image as well as interpolate. When downsampling an image with anti-aliasing the sampling filter kernel is scaled in order to properly anti-alias the input image signal. antialias has no effect when upsampling an image:

a = tf.image.resize(image, [5,10])
b = tf.image.resize(image, [5,10], antialias=True)
tf.reduce_max(abs(a - b)).numpy()
0.0

The method argument expects an item from the image.ResizeMethod enum, or the string equivalent. The options are:

  • bilinear: Bilinear interpolation. If antialias is true, becomes a hat/tent filter function with radius 1 when downsampling.
  • lanczos3: Lanczos kernel with radius 3. High-quality practical filter but may have some ringing, especially on synthetic images.
  • lanczos5: Lanczos kernel with radius 5. Very-high-quality filter but may have stronger ringing