Scikit Image - Scaling an image



Scaling an image refers to the process of resizing an image in computer graphics and digital image processing tasks. This includes making an image either larger (upscaling) or smaller (downscaling). And this process is also commonly used in various fields, like graphics design, web development, and multimedia.

Scaling images involves applying transformations that alter the size or resolution of an image. These transformations useful for various purposes, such as reducing the size of an image through down-sampling or enhancing its resolution through up-sampling. For instance the scikit-image library provides functions like transform.resize() and transform.rescale() to perform these scaling operations.

Using the skimage.transform.resize() function

The skimage.transform.resize() function allows you to resize an N-dimensional image to a specified output shape. This function performs interpolation to either up-size or down-size the image.

This function is particularly useful for resizing images while handling various interpolation methods and boundary conditions. It is recommended to enable anti-aliasing when downsizing images to avoid aliasing artifacts.

Syntax

Following is the syntax of this method −

skimage.transform.resize(image, output_shape, order=None, mode='reflect', cval=0, clip=True, preserve_range=False, anti_aliasing=None, anti_aliasing_sigma=None)

Parameters

  • image: The input image you want to resize (an N-dimensional NumPy array).
  • output_shape: The size of the generated output image, specified as an iterable (e.g., (rows, cols[, ][, dim])). If dim is not provided, the number of channels is preserved. If the number of input channels differs from the output channels, an n-dimensional interpolation is applied.
  • order (optional): The order of spline interpolation, ranging from 0 to 5. Default is 0 for boolean images and 1 otherwise.
  • mode (optional): Determines how points outside the input image boundaries are filled. Options include 'constant', 'edge', 'symmetric', 'reflect', and 'wrap'.
  • cval (optional): Used with the 'constant' mode to set the value outside the image boundaries.
  • clip (optional): Specifies whether to clip the output to the range of values in the input image. Enabled by default for higher-order interpolation methods.
  • preserve_range (optional): Determines whether to maintain the original value range. If False, the image is converted to float using the conventions of img_as_float.
  • anti_aliasing (optional): Specifies whether to apply Gaussian filtering before downsampling to avoid aliasing artifacts. If not specified, it is set to True when downsampling an image whose data type is not boolean. It is set to False when using nearest neighbor interpolation (order == 0) with integer input data type.
  • anti_aliasing_sigma (optional): The standard deviation for Gaussian filtering used when anti-aliasing. By default, this value is chosen as (s - 1) / 2, where s is the downsampling factor (s > 1). For the up-size case, s < 1, no anti-aliasing is performed prior to rescaling.

Return Value

It returns the resized version of the input image (NumPy array).

Example

The following example demonstrates how to use the skimage.transform.resize() to perform image resizing.

import numpy as np
from skimage import io, transform
import matplotlib.pyplot as plt

# Load the input image 
image = io.imread('Images/Flower1.jpg')

# Define the output shape to half the size of the original image
output_shape = (image.shape[0] // 2, image.shape[1] // 2)

# Resize the image 
resized_image = transform.resize(image, output_shape, anti_aliasing=True)

# Display the original and resized image
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image with shape '+str(image.shape))

axes[1].imshow(resized_image)
axes[1].set_title('Resized Image  with shape '+str(resized_image.shape))

# Show the plot
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Using the skimage.transform.rescale() function

This function allows you to scale an N-dimensional image by a specified factor. It performs interpolation to either up-scale or down-scale the image.

Syntax

Following is the syntax of this method −

skimage.transform.rescale(image, scale, order=None, mode='reflect', cval=0, clip=True, preserve_range=False, anti_aliasing=None, anti_aliasing_sigma=None, *, channel_axis=None)

Parameters

  • image: The input image you want to scale (an N-dimensional NumPy array).
  • scale: The scale factors, which can be a single float or a tuple of floats (e.g., (rows, cols[, ][, dim])) to specify separate scale factors for each dimension.
  • order (optional): The order of spline interpolation, ranging from 0 to 5. Default is 0 for boolean images and 1 for others.
  • mode (optional): Determines how points outside the input image boundaries are filled. Options include 'constant', 'edge', 'symmetric', 'reflect', and 'wrap'.
  • cval (optional): Used with the 'constant' mode to set the value outside the image boundaries.
  • clip (optional): Specifies whether to clip the output to the range of values in the input image. Enabled by default for higher-order interpolation methods.
  • preserve_range (optional): Determines whether to maintain the original value range. If False, the image is converted to float using the conventions of img_as_float.
  • anti_aliasing (optional): Specifies whether to apply Gaussian filtering before down-scaling to avoid aliasing artifacts. Not applied if the input image's data type is boolean.
  • anti_aliasing_sigma (optional): The standard deviation for Gaussian filtering. By default, it's chosen as (s - 1) / 2, where s is the down-scaling factor.
  • channel_axis (optional): Indicates the axis of the array that corresponds to the image channels. Use None for grayscale (single-channel) images. This parameter was added in version 0.19 of scikit-image.

Return Value

It returns a scaled version of the input image (NumPy array).

Example

The following example demonstrates how to use the skimage.transform.rescale() to perform image rescaling on a gray scale image.

import numpy as np
from skimage import io, transform
import matplotlib.pyplot as plt

# Load the input image as gray image
image = io.imread('Images/Flower1.jpg', as_gray=True)

# Define the scale factor
scale_factor = 0.25

# Rescale the image 
rescaled_image = transform.rescale(image, scale_factor, anti_aliasing=True)

# Display the original and rescaled image
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image with shape '+str(image.shape))

axes[1].imshow(rescaled_image)
axes[1].set_title('Rescaled Image  with shape '+str(rescaled_image.shape))

# Show the plot
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

When you load a 2D color image using rescale, it doesn't recognize that you have passed a color image. Instead, it assumes the image is an array of shape (100, 100, 3), where the last axis contains the red, green, and blue channels.

As a result, when you rescale the image without specifying the channel_axis parameter, it rescales along all axes, resulting in a new shape of (25, 25, 1) i.e, a grayscale image.

To avoid this issue and ensure that rescale works only across the rows and columns axes, without affecting the color channels, you should use the channel_axis parameter.

Example

The following example demonstrates how to use the skimage.transform.rescale() function on a color image to perform image rescaling operation.

import numpy as np
from skimage import io, transform
import matplotlib.pyplot as plt

# Load the input image 
image = io.imread('Images/Flower1.jpg')

# Define the scale factor
scale_factor = 0.25

# Rescale the image 
rescaled_image = transform.rescale(image, scale_factor, anti_aliasing=True, channel_axis=2)

# Display the original and rescaled image
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image with shape '+str(image.shape))

axes[1].imshow(rescaled_image)
axes[1].set_title('Rescaled Image  with shape '+str(rescaled_image.shape))

# Show the plot
plt.tight_layout()
plt.show()

Output

Advertisements