Gaussian smoothing reduces pixel-scale noise before thresholding, component labeling, or measurement. SciPy ndimage applies that smoothing directly to NumPy arrays, so image-processing code can blur a grayscale image without changing its array shape.
scipy.ndimage.gaussian_filter() uses sigma as the Gaussian standard deviation in pixels. A scalar sigma applies the same blur along every image axis, while a sequence can use different smoothing strengths for rows and columns.
Use a floating-point array when fractional filtered values matter. Integer image arrays can force the returned array and intermediate calculations into integer storage unless an output dtype is selected, so convert loaded 8-bit data before filtering and clip or cast only when saving the final image.
import numpy as np from scipy.ndimage import gaussian_filter image = np.zeros((7, 7), dtype=np.float32) image[2:5, 2:5] = np.array( [ [0.2, 0.7, 0.2], [0.7, 1.0, 0.7], [0.2, 0.7, 0.2], ], dtype=np.float32, ) filtered = gaussian_filter(image, sigma=1.0, mode="reflect") print(f"input_shape: {image.shape}") print(f"output_shape: {filtered.shape}") print(f"output_dtype: {filtered.dtype}") print(f"center_before_after: {image[3, 3]:.3f} -> {filtered[3, 3]:.3f}") print(f"corner_after: {filtered[0, 0]:.4f}") print("filtered_center_row:", np.array2string(filtered[3], precision=3, suppress_small=True)) print("filtered_center_column:", np.array2string(filtered[:, 3], precision=3, suppress_small=True))
For a real grayscale image, replace image with an array loaded by the image library already used by the project, then convert it with astype(np.float32) before filtering.
$ python gaussian_filter_image.py input_shape: (7, 7) output_shape: (7, 7) output_dtype: float32 center_before_after: 1.000 -> 0.476 corner_after: 0.0011 filtered_center_row: [0.025 0.133 0.349 0.476 0.349 0.133 0.025] filtered_center_column: [0.025 0.133 0.349 0.476 0.349 0.133 0.025]
image = np.zeros((7, 7), dtype=np.float32) image[2:5, 2:5] = np.array( [ [0.2, 0.7, 0.2], [0.7, 1.0, 0.7], [0.2, 0.7, 0.2], ], dtype=np.float32, )
Keep color images as separate channel arrays or filter only the spatial axes. Blurring across the channel axis mixes color channels.
filtered = gaussian_filter(image, sigma=1.0, mode="reflect")
mode=“reflect” mirrors values at the image edge. Increase sigma for stronger smoothing, or pass a tuple such as sigma=(1.0, 2.0) when rows and columns need different blur widths.
print(f"input_shape: {image.shape}") print(f"output_shape: {filtered.shape}") print(f"output_dtype: {filtered.dtype}")
gaussian_filter() returns an array with the same shape as the input. The dtype follows the selected output dtype, or the input dtype when no output dtype is selected.
print(f"center_before_after: {image[3, 3]:.3f} -> {filtered[3, 3]:.3f}") print("filtered_center_row:", np.array2string(filtered[3], precision=3, suppress_small=True)) print("filtered_center_column:", np.array2string(filtered[:, 3], precision=3, suppress_small=True))
The center value should drop while nearby pixels become nonzero, showing that the Gaussian kernel spread the bright center across neighboring pixels.
$ rm gaussian_filter_image.py