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:
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.
bicubic: Cubic interpolant of Keys. Equivalent to
Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel,
particularly when upsampling.
area: Anti-aliased resampling with area interpolation.
antialias has no effect when used with area interpolation; it
always anti-aliases.
mitchellcubic: Mitchell-Netravali Cubic non-interpolating filter.
For synthetic images (especially those lacking proper prefiltering), less
ringing than Keys cubic kernel but less sharp.
The return value has type float32, unless the method is
ResizeMethod.NEAREST_NEIGHBOR, then the return dtype is the dtype
of images:
Whether to preserve the aspect ratio. If this is set,
then images will be resized to a size that fits in size while
preserving the aspect ratio of the original image. Scales up the image if
size is bigger than the current size of the image. Defaults to False.
antialias
Whether to use an anti-aliasing filter when downsampling an
image.
name
A name for this operation (optional).
Raises
ValueError
if the shape of images is incompatible with the
shape arguments to this function
ValueError
if size has an 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].
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.image.resize\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/image_ops_impl.py#L1619-L1795) |\n\nResize `images` to `size` using the specified `method`. \n\n tf.image.resize(\n images,\n size,\n method=ResizeMethod.BILINEAR,\n preserve_aspect_ratio=False,\n antialias=False,\n name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Estimators](https://www.tensorflow.org/guide/estimator) | - [DeepDream](https://www.tensorflow.org/tutorials/generative/deepdream) - [pix2pix: Image-to-image translation with a conditional GAN](https://www.tensorflow.org/tutorials/generative/pix2pix) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) - [Image segmentation](https://www.tensorflow.org/tutorials/images/segmentation) - [Adversarial example using FGSM](https://www.tensorflow.org/tutorials/generative/adversarial_fgsm) |\n\nResized images will be distorted if their original aspect ratio is not\nthe same as `size`. To avoid distortions see\n[`tf.image.resize_with_pad`](../../tf/image/resize_with_pad). \n\n image = tf.constant([\n [1,0,0,0,0],\n [0,1,0,0,0],\n [0,0,1,0,0],\n [0,0,0,1,0],\n [0,0,0,0,1],\n ])\n # Add \"batch\" and \"channels\" dimensions\n image = image[tf.newaxis, ..., tf.newaxis]\n image.shape.as_list() # [batch, height, width, channels]\n [1, 5, 5, 1]\n tf.image.resize(image, [3,5])[0,...,0].numpy()\n array([[0.6666667, 0.3333333, 0. , 0. , 0. ],\n [0. , 0. , 1. , 0. , 0. ],\n [0. , 0. , 0. , 0.3333335, 0.6666665]],\n dtype=float32)\n\nIt works equally well with a single image instead of a batch of images: \n\n tf.image.resize(image[0], [3,5]).shape.as_list()\n [3, 5, 1]\n\nWhen `antialias` is true, the sampling filter will anti-alias the input image\nas well as interpolate. When downsampling an image with [anti-aliasing](https://en.wikipedia.org/wiki/Spatial_anti-aliasing) the sampling filter\nkernel is scaled in order to properly anti-alias the input image signal.\n`antialias` has no effect when upsampling an image: \n\n a = tf.image.resize(image, [5,10])\n b = tf.image.resize(image, [5,10], antialias=True)\n tf.reduce_max(abs(a - b)).numpy()\n 0.0\n\nThe `method` argument expects an item from the [`image.ResizeMethod`](../../tf/image/ResizeMethod) enum, or\nthe string equivalent. The options are:\n\n- **`bilinear`** : [Bilinear interpolation.](https://en.wikipedia.org/wiki/Bilinear_interpolation) If `antialias` is true, becomes a hat/tent filter function with radius 1 when downsampling.\n- **`lanczos3`** : [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling) with radius 3. High-quality practical filter but may have some ringing, especially on synthetic images.\n- **`lanczos5`** : [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling) with radius 5. Very-high-quality filter but may have stronger ringing.\n- **`bicubic`** : [Cubic interpolant](https://en.wikipedia.org/wiki/Bicubic_interpolation) of Keys. Equivalent to Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel, particularly when upsampling.\n- **`gaussian`** : [Gaussian kernel](https://en.wikipedia.org/wiki/Gaussian_filter) with radius 3, sigma = 1.5 / 3.0.\n- **`nearest`** : [Nearest neighbor interpolation.](https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation) `antialias` has no effect when used with nearest neighbor interpolation.\n- **`area`** : Anti-aliased resampling with area interpolation. `antialias` has no effect when used with area interpolation; it always anti-aliases.\n- **`mitchellcubic`**: Mitchell-Netravali Cubic non-interpolating filter. For synthetic images (especially those lacking proper prefiltering), less ringing than Keys cubic kernel but less sharp.\n\n| **Note:** Near image edges the filtering kernel may be partially outside the image boundaries. For these pixels, only input pixels inside the image will be included in the filter sum, and the output value will be appropriately normalized.\n\nThe return value has type `float32`, unless the `method` is\n[`ResizeMethod.NEAREST_NEIGHBOR`](../../tf/image/ResizeMethod#NEAREST_NEIGHBOR), then the return dtype is the dtype\nof `images`: \n\n nn = tf.image.resize(image, [5,7], method='nearest')\n nn[0,...,0].numpy()\n array([[1, 0, 0, 0, 0, 0, 0],\n [0, 1, 1, 0, 0, 0, 0],\n [0, 0, 0, 1, 0, 0, 0],\n [0, 0, 0, 0, 1, 1, 0],\n [0, 0, 0, 0, 0, 0, 1]], dtype=int32)\n\nWith `preserve_aspect_ratio=True`, the aspect ratio is preserved, so `size`\nis the maximum for each dimension: \n\n max_10_20 = tf.image.resize(image, [10,20], preserve_aspect_ratio=True)\n max_10_20.shape.as_list()\n [1, 10, 10, 1]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `images` | 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor of shape `[height, width, channels]`. |\n| `size` | A 1-D int32 Tensor of 2 elements: `new_height, new_width`. The new size for the images. |\n| `method` | An [`image.ResizeMethod`](../../tf/image/ResizeMethod), or string equivalent. Defaults to `bilinear`. |\n| `preserve_aspect_ratio` | Whether to preserve the aspect ratio. If this is set, then `images` will be resized to a size that fits in `size` while preserving the aspect ratio of the original image. Scales up the image if `size` is bigger than the current size of the `image`. Defaults to False. |\n| `antialias` | Whether to use an anti-aliasing filter when downsampling an image. |\n| `name` | A name for this operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------------------------------|\n| `ValueError` | if the shape of `images` is incompatible with the shape arguments to this function |\n| `ValueError` | if `size` has an invalid shape or type. |\n| `ValueError` | if an unsupported resize method is specified. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| 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]`. ||\n\n\u003cbr /\u003e"]]