Open In App

Alternative to scipy.misc.imresize() in Python

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the alternatives of scipy.misc.imresize() method in Python. But why do we need any alternative to that? The reason behind this is that in the new stable releases of the scipy this function has been deprecated and is no longer available. Let’s see these alternatives one by one in the next section.

Using Skimage to Resize Images

Skimage package in python provides functions for image processing. Skimage package provides a transform module that provides a resize function as skimage.transform.resize() to obtain the image of desired resized size. This function accepts the original image as a parameter and the desired size and returns the resized image. This function has the following syntax:

First, let’s import some libraries and define a utility function to plot the original image side by side with the resized image.

Python3




# Importing libraries
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
  
def plot_image(img1, img2):
    plt.subplot(1, 2, 1)
  
    plt.imshow(img1)
    plt.title(img1.size)
  
    plt.subplot(1, 2, 2)
    plt.imshow(img2)
    plt.title(img2.size)
  
    plt.show()


Example:

In this example, we are trying to resize the image “gfg.png” by 0.7 times its original size using Pillow.

Python3




from skimage import io, transform
  
image = io.imread("gfg.png")
  
# Obtain the size of the image
h, w, c = image.shape
  
# Resize the image to 0.7 times of it's original size
h_new, w_new = (int)(h * 0.7), (int)(w * 0.7)
  
# new resized image
resizedImage = transform.resize(image, (h_new, w_new))
plot_image(image, resizedImage)


Output:

Resized image using Skimage

Original image v/s resized image

Using Pillow to Resize Images

Pillow is a part of PIL. PIL stands for “python imagine library” which contains functions to manipulate images in Python. The image module of Pillow allows us to resize the image very easily. The very first step is to open the image with the help of the function: PIL.Image.open(). Then we can resize the image using PIL.Image.resize() function.

Example:

In this example, we are trying to resize the image “gfg.png” (saved locally at C:\Users\harsh\Desktop\) by 0.7 times its original size using Skimage.

Python3




# Open the image
image = Image.open("gfg.png")
  
# Obtain the size of the image
h, w = image.size
  
# Resize the image to 0.7 times of it's original size
h_new, w_new = (int)(h * 0.7), (int)(w * 0.7)
  
# new resized image
resizedImage = image.resize((h_new, w_new))
plot_image(image, resizedImage)


Output:

Resized image using Pillow

Original image v/s resized image

Using OpenCV to Resize Images

OpenCV is also an image-processing library that provides highly optimized functions to perform a complex task in a single line of code. 

Python3




import cv2
  
# Loading the image
image = cv2.imread("gfg.png")
  
# Get the size of the image
h, w, c = image.shape
  
# Resize the image to 0.7 times of it's original size
h_new, w_new = (int)(h * 0.7), (int)(w * 0.7)
  
# new resized image
resizedImage = cv2.resize(image, (w_new, h_new))
plot_image(image, resizedImage)


Output:

Resized image using OpenCV

Original image v/s resized image

One thing you may wonder is why the third channel size is different based on the different libraries we choose. A simple answer to this is due to the difference between the format in which images are converted into arrays of pixels. Whether it is RGB or black and white or CMYK



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads