Resizing

The resizing Ops accept input images as tensors of several types. They always output resized images as float32 tensors.

The convenience function resize_images() supports both 4-D and 3-D tensors as input and output. 4-D tensors are for batches of images, 3-D tensors for individual images.

Other resizing Ops only support 4-D batches of images as input: resize_area, resize_bicubic, resize_bilinear, resize_nearest_neighbor.

Example:

# Decode a JPG image and resize it to 299 by 299 using default method.
image = tf.image.decode_jpeg(...)
resized_image = tf.image.resize_images(image, [299, 299])

tf.image.resize_images(images, size, method=0, align_corners=False)

Resize images to size using the specified method.

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

method can be one of:

  • ResizeMethod.BILINEAR: [Bilinear interpolation.] (https://en.wikipedia.org/wiki/Bilinear_interpolation)
  • ResizeMethod.NEAREST_NEIGHBOR: [Nearest neighbor interpolation.] (https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation)
  • ResizeMethod.BICUBIC: [Bicubic interpolation.] (https://en.wikipedia.org/wiki/Bicubic_interpolation)
  • ResizeMethod.AREA: Area interpolation.
Args:
  • images: 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels].
  • size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.
  • method: ResizeMethod. Defaults to ResizeMethod.BILINEAR.
  • align_corners: bool. If true, exactly align all 4 corners of the input and output. Defaults to false.
Raises:
  • ValueError: if the shape of images is incompatible with the shape arguments to this function
  • ValueError: if size has invalid shape or type.
  • ValueError: if an unsupported resize method is specified.
Returns:

If images was 4-D, a 4-D float Tensor of shape [batch, new_height, new_width, channels]. If images was 3-D, a 3-D float Tensor of shape [new_height, new_width, channels].


tf.image.resize_area(images, size, align_corners=None, name=None)

Resize images to size using area interpolation.

Input images can be of different types but output images are always float.

Args:
  • images: A Tensor. Must be one of the following types: uint8, int8, int16, int32, int64, half, float32, float64. 4-D with shape [batch, height, width, channels].
  • size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.
  • align_corners: An optional bool. Defaults to False. If true, rescale input by (new_height - 1) / (height - 1), which exactly aligns the 4 corners of images and resized images. If false, rescale by new_height / height. Treat similarly the width dimension.
  • name: A name for the operation (optional).
Returns:

A Tensor of type float32. 4-D with shape [batch, new_height, new_width, channels].


tf.image.resize_bicubic(images, size, align_corners=None, name=None)

Resize images to size using bicubic interpolation.

Input images can be of different types but output images are always float.

Args:
  • images: A Tensor. Must be one of the following types: uint8, int8, int16, int32, int64, half, float32, float64. 4-D with shape [batch, height, width, channels].
  • size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.
  • align_corners: An optional bool. Defaults to False. If true, rescale input by (new_height - 1) / (height - 1), which exactly aligns the 4 corners of images and resized images. If false, rescale by new_height / height. Treat similarly the width dimension.
  • name: A name for the operation (optional).
Returns:

A Tensor of type float32. 4-D with shape [batch, new_height, new_width, channels].


tf.image.resize_bilinear(images, size, align_corners=None, name=None)

Resize images to size using bilinear interpolation.

Input images can be of different types but output images are always float.

Args:
  • images: A Tensor. Must be one of the following types: uint8, int8, int16, int32, int64, half, float32, float64. 4-D with shape [batch, height, width, channels].
  • size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.
  • align_corners: An optional bool. Defaults to False. If true, rescale input by (new_height - 1) / (height - 1), which exactly aligns the 4 corners of images and resized images. If false, rescale by new_height / height. Treat similarly the width dimension.
  • name: A name for the operation (optional).
Returns:

A Tensor of type float32. 4-D with shape [batch, new_height, new_width, channels].


tf.image.resize_nearest_neighbor(images, size, align_corners=None, name=None)

Resize images to size using nearest neighbor interpolation.

Args:
  • images: A Tensor. Must be one of the following types: uint8, int8, int16, int32, int64, half, float32, float64. 4-D with shape [batch, height, width, channels].
  • size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.
  • align_corners: An optional bool. Defaults to False. If true, rescale input by (new_height - 1) / (height - 1), which exactly aligns the 4 corners of images and resized images. If false, rescale by new_height / height. Treat similarly the width dimension.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as images. 4-D with shape [batch, new_height, new_width, channels].