Search code examples
numpytensorflowimage-preprocessing

Is there a better way to resize an image that is in the form of a numpy array?


I'm new to neural networks, and have been practicing image preprocessing. I am trying to resize images that are in the form of numpy arrays, and this is my current method:

# using tensorflow, resize the image
im = tf.image.resize(img_as_array, [299, 299])

# then use .eval() which returns a numpy array in the size I want
im_arr = im.eval(session=tf.compat.v1.Session())

Is there a better way to do this?


Solution

  • To avoid having to convert between tf.tensor and np.ndarray tensor data types, you can use skimage (scikit-image) to resize the image directly on np.ndarray objects using the following code segment:

    import skimage.transform
    
    kwargs = dict(output_shape=self._size, mode='edge', order=1, preserve_range=True)
    im = skimage.transform.resize(im, **kwargs).astype(im.dtype)
    

    To install skimage, follow the installation instructions here: https://pypi.org/project/scikit-image/. Hope this helps!