Clash
Clash

Reputation: 5025

How do I draw a resized image in TensorFlow?

It seems like images in TensorFlow get transformed to a different kind of image coordinate system after any transformation (e.g. resize) is applied. Drawing the image results in this:

enter image description here

%matplotlib inline
import tensorflow as tf
from matplotlib import pyplot as plt

with tf.device("/cpu:0"):
    file_contents = tf.read_file('any_image.png')
    image = tf.image.decode_png(file_contents)
    image.set_shape([375, 1242, 3])
    image = tf.image.resize_images(image, 448, 448)

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        image_val = sess.run([image])
        plt.figure(figsize=(16, 8))
        plt.imshow(image_val[0], interpolation='nearest')
        plt.show()
        plt.close()                                                

If I remove the resize operation it draws the regular image. How do I get matplotlib to draw the resized image correctly or tell Tensorflow to return it to RGB?

Upvotes: 2

Views: 1192

Answers (1)

Clash
Clash

Reputation: 5025

Seems like there is no image transformation besides unsigned integer to float. Converting back to unsigned integer fixed the problem.

plt.imshow(image_val[0].astype(np.uint8), interpolation='nearest')

enter image description here

Upvotes: 4

Related Questions