How to Resize Image in TensorFlow
Images can be manipulated through libraries like OpenCV, Pillow, TensorFlow, etc. TensorFlow is more focused due to constant updates and better compatibility with the GPUs.
TensorFlow version 1x and version 2x have different classes and methods to perform multiple image operations. Some methods have been syntactically updated in version 2x.
In the following section, we will demonstrate two examples. One would be dealt with version 2x on a single image.
The next example will include TensorFlow version 1x with a series of images. Let’s jump into that part.
Resize a Single Image in TensorFlow
In this case, we have used Google Colab as our environment. This is to specify that Google Colab does not support TensorFlow version 1x and only imports version 2x.
Initially, the drive was mounted, and thus the importation of images was enabled.
After taking the image path, we loaded it as a TensorFlow image. The resize
operation will not occur on multiple image formats; instead, they must be converted into an array with corresponding pixel values.
In the next phase, we will declare a specific height and width for the image and pass those values in the resize()
method of TensorFlow. Later we will again convert the arrays to an image and plot it to visualize if the image quality varied on resizing.
Code Snippet:
from google.colab import drive
drive.mount("/content/drive")
import tensorflow as tf
import matplotlib.pyplot as plt
image_path = "/content/drive/MyDrive/IMG/pizza.jpg"
img = tf.keras.utils.load_img(image_path)
plt.imshow(img)
print(img.size)
image_array = tf.keras.preprocessing.image.img_to_array(img)
new_img = tf.image.resize(image_array, (70, 70))
resized_img = tf.keras.preprocessing.image.array_to_img(new_img)
plt.imshow(resized_img)
Output:
Resize a Series of Images in TensorFlow
We will use the Jupyter Notebook in this example. The dependencies and methods required are available in TensorFlow version 1x.
Though those methods are constantly updating in version 2x, we preferred using the previous one for smooth operations.
We have installed the necessary packages and imported them. The TensorFlow version, in this case, is 1.15.8
.
In this case, the images are fetched as strings and later passed to the tf.train.string_input_producer()
we get a queue of files.
After initializing a file reader, we will receive the names and corresponding contents. The contents are later decoded into the jpeg
format.
Usually, TensorFlow uses discrete cosine transform as the default for its inputs. Then, we typecast the images to float and finally resize them.
Code Snippet:
import matplotlib.pyplot as plt
import tensorflow as tf
import os
tf.__version__
image_path = "D:M"
categ = ["coffee"]
for c in categ:
path = os.path.join(image_path, c)
for img in os.listdir(path):
image = [os.path.join(path, img)]
queue = tf.train.string_input_producer(image)
reader = tf.WholeFileReader()
name, content = reader.read(queue)
images = tf.image.decode_jpeg(content, channels=3)
print(images.shape)
images = tf.cast(images, tf.float32)
resized_images = tf.image.resize_images(images, (224, 224))
print(resized_images.shape)
Output: