nmorsi
nmorsi

Reputation: 23

how to display resized image in python

I've resize an image but I couldn't display it using matplotlib here is my code

!wget 'https://machinelearningmastery.com/wp-content/uploads/2019/01/Sydney-Opera-House.jpg'


from matplotlib import image
from matplotlib import pyplot
import tensorflow as tf

d=tf.image.resize(
    images=data,
    size=[700,700],
    method=tf.image.ResizeMethod.BILINEAR,
    preserve_aspect_ratio=False,
    antialias=False,
    name=None
)
pyplot.imshow(d)
pyplot.show()

I used another method as follows

d=tf.image.resize_with_pad(
    image=data,
    target_height=700,
    target_width=700,
    method=tf.image.ResizeMethod.GAUSSIAN,
    antialias=False
)

but the output is always white image plot whether I'm shrinking or enlarging the image dimensions

any idea how can i display the resized image

Upvotes: 1

Views: 682

Answers (1)

My Work
My Work

Reputation: 2518

I guess you have received a warning saying:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

That tells you that for your array d, all the values were scaled to be between 0 and 1. As the tensorflow docs say:

The return value has type float32, unless the method is ResizeMethod.NEAREST_NEIGHBOR, then the return dtype is the dtype of images

The way to avoid it is to convert the array you resided to be integers. I am not sure what you do and why you need both tensor flow and matplotlib but a simple fix is:

from matplotlib import pyplot
import tensorflow as tf
import numpy as np

data = pyplot.imread('./Sydney-Opera-House.jpg')

d=tf.image.resize(
    images=data,
    size=[700,700],
    method=tf.image.ResizeMethod.BILINEAR,
    preserve_aspect_ratio=False,
    antialias=False,
    name=None
)

pyplot.imshow(np.array(d, dtype=int))
pyplot.show()

The other is to use what the docs say:

d=tf.image.resize(
    images=data,
    size=[700,700],
#     method=tf.image.ResizeMethod.BILINEAR,
    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
    preserve_aspect_ratio=False,
    antialias=False,
    name=None
)

but again, I don't know what you want to do so it's hard to give advice.

enter image description here

Upvotes: 1

Related Questions