Skip to content

Instantly share code, notes, and snippets.

@Prasad9
Last active May 9, 2019 23:40
Show Gist options
  • Save Prasad9/c7fe4a929db1eae1c8d84768323ade01 to your computer and use it in GitHub Desktop.
Save Prasad9/c7fe4a929db1eae1c8d84768323ade01 to your computer and use it in GitHub Desktop.
Resize images to a common size
import tensorflow as tf
import matplotlib.image as mpimg
import numpy as np
IMAGE_SIZE = 224
def tf_resize_images(X_img_file_paths):
X_data = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, (None, None, 3))
tf_img = tf.image.resize_images(X, (IMAGE_SIZE, IMAGE_SIZE),
tf.image.ResizeMethod.NEAREST_NEIGHBOR)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Each image is resized individually as different image may be of different size.
for index, file_path in enumerate(X_img_file_paths):
img = mpimg.imread(file_path)[:, :, :3] # Do not read alpha channel.
resized_img = sess.run(tf_img, feed_dict = {X: img})
X_data.append(resized_img)
X_data = np.array(X_data, dtype = np.float32) # Convert to numpy
return X_data
@PeculiarJukebox
Copy link

Hello. I am new to coding so I don't know much about anything. I tried your code and it works without any errors but I can't resize any images (I can't choose images to resize using the code.). Can you help me on using this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment