'Matplotlib plot becomes blank after tf.image.resize

I have some code that I am using with tensorflow datasets. It's worked fine previously and it may still work. But I don't think so

img = parse_image(img_paths[0])
img = tf.image.resize(img, [224, 224])
plt.imshow(img)

Just outputs a blank 224x224 canvas.

img = parse_image(img_paths[0])
plt.imshow(img)

outputs the image correctly.

img_paths is a list of strings with pathnames

I have tried:

img = parse_image(img_paths[0])
img = tf.image.resize([img], [224, 224])
plt.imshow(img[0])

and

img = parse_image(img_paths[0])
img = tf.image.resize(img, [224, 224])
plt.imshow(img.numpy())

and

img = parse_image(img_paths[0])
img = tf.image.resize([img], [224, 224])
plt.imshow(img.numpy()[0])

The shape is correct and this code has worked before. And may still work, I'm thinking I may not use it correctly anymore (been a while since I wrote it).

thanks for any hints or thoughts you can provide? And of course solutions ;-)



Solution 1:[1]

Huh,

I saw something elsewhere and added this line:

img = tf.image.convert_image_dtype(img, tf.float32)

before resizing and it worked.

This is extremely weird because I didn't need this line before. Maybe due to a version update?

Either way this works:

img = parse_image(train_img_paths[0])
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(img, [224, 224])
plt.imshow(img)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olli