Search code examples
pythonmatplotlibscikit-image

The changing a color of image (rescale). Scikit-image


Why does the color of image change when I use rescale in scikit image?

enter image description here

import matplotlib.pyplot as plt
from skimage.transform import rescale

im = imread('C:/abc.jpg')
plt.imshow(im)
plt.show()

im_rescaled = rescale(im , 0.25)
plt.imshow(im_rescaled)
plt.show() 

Solution

  • rescale doesn't know that you have passed a 2D color image. When you load it in, it is an array of shape (100, 100, 3), where the 3 elements of the final axis are the red, green, and blue channels. rescale rescales along all axes by default, so you end up with an image of shape (25, 25, 1). Matplotlib ignores this final axis and renders it as a grayscale image using its default colormap, viridis, which is why it looks green.

    In order to make rescale work only across the rows and column axes, and not along the channels axis, use

    im_rescaled = rescale(im, 0.25, multichannel=True)
    

    This will give you a rescaled image of shape (25, 25, 3) will all three color channels as expected.

    You can read more about the rescale API in the scikit-image API docs.