Alternative to scipy.misc.imresize() in Python

In this short tutorial, we will see what the problem with scipy.misc.imresize() in Python is which was previously used to resize an image. Next, we will see some alternatives to this function. Let’s get started.

 

scipy.misc.imresize()

This function resizes an image. The function is deprecated in Scipy version 1.0.0 and removed in 1.3.0. If you read the official Scipy documentation of Scipy 1.0.0 for scipy.misc.imresize() and the source code for this function, you will see the deprecation message. And, when you check the official documentation of the stable release Scipy 1.4.1, you will see that the function no longer exists under the miscellaneous routines(sipy.misc).

First, let’s see the code snippet used to resize the image using scipy.misc.imresize() in Scipy version 1.0.0.

import cv2
import scipy

# read an image
image = cv2.imread(r"C:\Users\Admin\Pictures\kangaroo.jpg")

# resize the image to half of it's original size
resized_image = scipy.misc.resize(image, 0.5, interp = 'cubic')

 

Note that this code snippet will no longer work as the ‘resize’ attribute from misc has been removed in stable releases. You will see the following error message –

AttributeError: module 'scipy.misc' has no attribute 'resize'

Now, Scipy suggests some alternatives to this function. Let’s see these alternatives one by one in the next section.

 

Alternatives to scipy.misc.imresize()

1. Pillow 

Pillow is a PIL fork. PIL or Python Imaging Library is a Python package that provides functions to manipulate images in Python. We can resize the image using the Image module of Pillow. First, open the image using PIL.Image.open(). Then to resize the image, we can use PIL.Image.resize().

The function takes as parameters, the size of the image as a 2-tuple. An optional parameter is a resampling filter. The default resampling filter is PIL.Image.BICUBIC. There are other two optional parameters – box and reducing_gap.

import numpy as np
from PIL import Image

# open the image
image = Image.open(r"C:\Users\Admin\Pictures\kangaroo.jpg")
# get the size of the image
size = np.array(image.size)
# resize the image to half of it's original size
new_size = tuple((size*0.5).astype(int))
# new resized image
resized_image = im.resize(new_size,Image.BICUBIC)

print("Size of original image: {}".format(image.size))
print("Size of new resized image: {}".format(resized_image.size))
Size of original image: (650, 433)
Size of new resized image: (325, 216)

 

2. Skimage

Skimage is a Python package that provides functions for image processing. The transform module of the Skimage package provides a resize function skimage.transform.resize() to get the desired resized size. The function takes as parameters the original image and the required output size and returns the resized image. Read the image using skimage.io.imread() and then resize the image.

import numpy as np
import skimage

# read the imge
image = skimage.io.imread(r"C:\Users\Admin\Pictures\kangaroo.jpg")
# get the size of the image
size = np.array(image.shape)
# resize the image to half of it's original size
new_size = (size[:2]*0.5).astype(int)
# new rsized iamge
resized_image = skimage.transform.resize(image, new_size)

print("Size of original image: {}".format(image.shape))
print("Size of new resized image: {}".format(resized_image.shape))
Size of original image: (433, 650, 3)
Size of new resized image: (216, 325, 3)

 

Want to add your thoughts? Need any further help? Leave a comment and I will get back to you ASAP 🙂

For further reading:

 

Leave a Reply

Your email address will not be published.