How can I prevent Numpy/ SciPy gaussian blur from converting image to grey scale?

a is a 3-d array with shape (M, N, 3). The problem is that ndimage.uniform_filter(a, size=11) applies a filter with length 11 to each dimension of a, include the third axis that holds the color channels. When you apply the filter with length 11 to an axis with length 3, the resulting values are all pretty close to the average of the three values, so you get something pretty close to a gray scale. (Depending on the image, you might have some color left.)

What you actually want is to apply a 2-d filter to each color channel separately. You can do this by giving a tuple as the size argument, using a size of 1 for the last axis:

a_g_blure = ndimage.uniform_filter(a, size=(11, 11, 1))

Note: uniform_filter is not a Gaussian blur. For that, you would use scipy.ndimage.gaussian_filter. You might also be interested in the filters provided by scikit-image. In particular, see skimage.filters.gaussian_filter.


For a gaussian blur, I recommend using skimage.filters.gaussian_filter.

from skimage.io import imread
from skimage.filters import gaussian_filter

sigma=5  # blur radius

img = imread('path/to/img')

# this will only return grayscale
grayscale_blur = gaussian_filter(src_img, sigma=sigma)

# passing multichannel param as True returns colors
color_blur = gaussian_filter(src_img, sigma=sigma, multichannel=True)