Resizing

Use TensorFlow to resize images with a variety of different image scaling algorithms.

Chapter Goals:

  • Be able to resize pixel data when required
  • Understand how resizing works in TensorFlow

A. Basic resizing

The function we use for resizing pixel data is tf.image.resize. It takes in two required arguments: the original image's decoded data and the new size of the image, which is a tuple/list of two integers representing new_height and new_width, in that order.

Press + to interact
import tensorflow as tf
with tf.compat.v1.Session() as sess:
print('Original: {}'.format(
repr(sess.run(decoded_image)))) # Decoded image data
resized_img = tf.image.resize(decoded_image, (3, 2))
print('Resized: {}'.format(
repr(sess.run(resized_img))))

The function compresses or expands the image (depending on the relationship between the new image dimensions and the old image dimensions) and then returns the pixel data for the resized image, with the same number of channels. Note that if the resized dimensions don't match the same aspect ratio as the ...