Skip to main content

How to Resize an Image in TensorFlow with tf.image.resize

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Photos come in all sizes, and a model wants every one the same size. tf.image.resize changes an image's height and width, working out the new pixels from the old ones. This guide enlarges a tiny 2 × 2 picture in TensorFlow 2.22.0, so every new pixel value can be checked by eye.

Syntax​

Syntax
tf.image.resize(<images>, [<height>, <width>], method=<method>)
PartWhat it means
<images>One image of shape (height, width, channels), or a batch of shape (batch, height, width, channels)
[<height>, <width>]The new size in pixels
method=<method>Optional. How the new pixels are worked out. The default is "bilinear", which blends the nearest pixels; "nearest" copies the nearest pixel
ResultThe resized images, as float32, except with "nearest", which keeps the input's type

The tf.image.resize API page says it will "Resize images to size using the specified method", and about the result: "The return value has type float32, unless the method is ResizeMethod.NEAREST_NEIGHBOR, then the return dtype is the dtype of images". It also warns: "Resized images will be distorted if their original aspect ratio is not the same as size."

The Example​

A gray picture 2 pixels high and 2 pixels wide, with the values 0 and 100 in the top row and 200 and 255 in the bottom row. The script enlarges it to 4 × 4 twice: with the default method, then with "nearest".

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

resize_demo.py
import tensorflow as tf

# A picture 2 pixels high and 2 pixels wide, with one gray value per pixel
image = tf.constant([[[0], [100]], [[200], [255]]], dtype=tf.uint8)

bigger = tf.image.resize(image, [4, 4])
print(bigger.shape, bigger.dtype)
print(bigger[:, :, 0].numpy())

nearest = tf.image.resize(image, [4, 4], method="nearest")
print(nearest.shape, nearest.dtype)
print(nearest[:, :, 0].numpy())

The resize_demo.py script in a macOS-style code window: a 2 by 2 uint8 gray picture with the values 0, 100, 200 and 255, resized to 4 by 4 with tf.image.resize, once with the default method and once with method nearest, printing each result's shape, dtype and values.

The picture has the shape (2, 2, 1): 2 rows, 2 pixels, 1 gray value each. [:, :, 0] takes that one value from every pixel, so the results print as plain 4 × 4 tables.

Step 2: Run It​

Terminal
uv run python resize_demo.py
Output
(4, 4, 1) <dtype: 'float32'>
[[ 0. 25. 75. 100. ]
[ 50. 72.1875 116.5625 138.75 ]
[150. 166.5625 199.6875 216.25 ]
[200. 213.75 241.25 255. ]]
(4, 4, 1) <dtype: 'uint8'>
[[ 0 0 100 100]
[ 0 0 100 100]
[200 200 255 255]
[200 200 255 255]]

The terminal after uv run python resize_demo.py: the bilinear result is float32 with blended values such as 25, 72.1875 and 241.25, and the nearest result is uint8 with each original value repeated in a 2 by 2 block.

The default method kept the four original values in the corners and blended the pixels in between: in the top row, 25 is a quarter of the way from 0 to 100 and 75 three quarters. The result is float32, even though the picture was uint8. With "nearest", every new pixel copied the nearest original one, so each value became a 2 × 2 block and the type stayed uint8. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Resizing an image without a channel axis, shape (2, 2)ValueError: 'images' must have either 3 or 4 dimensions.Add the axis: tf.image.resize(image[:, :, tf.newaxis], [4, 4]) returned the shape (4, 4, 1)
Treating the result as uint8It's float32. Passing it to tf.io.encode_jpeg stopped with InvalidArgumentError: cannot compute EncodeJpeg as input #0(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:EncodeJpeg]Convert it: tf.cast(bigger, tf.uint8) dropped the fractions, so 72.1875 became 72
Resizing to a different shape than the photo'sA picture 32 pixels high and 64 wide, resized to [100, 100], became (100, 100, 1): its height grew twice as much as its width, stretching itUse tf.image.resize_with_pad, or preserve_aspect_ratio=True, as the FAQ below shows

FAQ​

How do I resize without stretching the picture?​

tf.image.resize_with_pad(image, 100, 100) resized that 32 × 64 picture to fit, keeping its shape, and filled the rest with black: the result was (100, 100, 1), with 0.0 in the top row and the picture's own values in the middle. Its docstring says it resizes "by keeping the aspect ratio the same without distortion", then the image "is resized and then padded with zeroes to match requested dimensions". If you don't need an exact size, tf.image.resize(image, [100, 100], preserve_aspect_ratio=True) returned (50, 100, 1) instead: as big as fits, without padding.

Can I resize a whole batch of images at once?​

Yes. A 4-D tensor is a batch: two copies of the picture, shape (2, 2, 2, 1), became (2, 4, 4, 1).

Conclusion​

tf.image.resize turned a 2 × 2 picture into a 4 × 4 one. The default method blended new pixels from the old ones and returned float32; "nearest" copied them and kept uint8. The image needs a channel axis, and a size with a different shape from the image's stretches it unless you pad it.

Additional Resources​