Gaussian Blur Filters

Gaussian Blur Kernel Filter in Python

         The Blur Box image filters use pixel values averaging based on a uniform distribution, the alternative approach employs a pixel-wise average within a specified neighborhood. In contrast to a Blur Box, the Gaussian blur leverages a Gaussian distribution, intricately assigning weights to pixels based on their proximity to the center. This results in a more refined and natural blurring effect, as pixels nearer to the center carry higher weights. The following 5x5 matrix can be used for the Gaussian approximation:

# gaussian blur 5x5 kernel
[
 [1, 4,  6,  4,  1],
 [4, 16, 24, 16, 4],
 [6, 24, 36, 24, 6],
 [4, 16, 24, 16, 4],
 [1, 4,  6,  4,  1],
]

         To maintain the luminosity of the source image, it is imperative to divide all elements by 256, representing the sum of the matrix elements.

Gaussian blur, in contrast to the uniform blurring observed in typical blur filters, tends to yield a visually more appealing and realistic result.

Original image
Gaussian blur
Original vector image
Gaussian blur 5x5 kernel filter in Python
Gaussian blur kernel filter

Python code example

         The provided Python code example serves as an illustration of the Aspose.Imaging Python API's usage. Utilize the `ConvolutionFilter` class, which provides pre-defined kernel filters, including the get_gaussian() method with customizable size and sigma values for Gaussian distribution. Moreover, you retain the flexibility to craft your personalized kernel matrix. Within this example, image templates in raster PNG and as well as vector SVG formats are loaded from the "templates" folder, and a set of filters are applied from the `kernel_filters` list.

import os, random
import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage, VectorImage
from aspose.imaging.imagefilters.complexutils import Complex
from aspose.imaging.imagefilters.convolution import *
from aspose.imaging.imagefilters.filteroptions import *
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
def delete_file(file):
if delete_output:
os.remove(file)
# Example code:
def filter(raster, options, output_path):
raster.filter(raster.bounds, options)
raster.save(output_path)
def get_random_kernel(cols, rows):
custom_kernel = [0.0] * (cols * rows)
for y in range(rows):
for x in range(cols):
custom_kernel[y*cols + x] = random.random()
return custom_kernel
size: int = 5
sigma: float = 1.5
angle: float = 45
custom_kernel = get_random_kernel(size, 7)
custom_complex = ConvolutionFilter.to_complex(custom_kernel)
kernel_filters = [
# convolution filters
ConvolutionFilterOptions(ConvolutionFilter.get_emboss_3x3()),
ConvolutionFilterOptions(ConvolutionFilter.get_emboss_5x5()),
ConvolutionFilterOptions(ConvolutionFilter.get_sharpen_3x3()),
ConvolutionFilterOptions(ConvolutionFilter.get_sharpen_5x5()),
ConvolutionFilterOptions(ConvolutionFilter.get_blur_box(size)),
ConvolutionFilterOptions(ConvolutionFilter.get_blur_motion(size, angle)),
ConvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma)),
ConvolutionFilterOptions(custom_kernel),
GaussianBlurFilterOptions(size, sigma),
SharpenFilterOptions(size, sigma),
MedianFilterOptions(size),
# deconvolution filters
DeconvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma)),
DeconvolutionFilterOptions(custom_kernel),
DeconvolutionFilterOptions(custom_complex),
GaussWienerFilterOptions(size, sigma),
MotionWienerFilterOptions(size, sigma, angle)
]
data_dir = templates_folder
input_paths = [
os.path.join(data_dir, "template.png"),
os.path.join(data_dir, "template.svg")
]
outputs = []
for input_path in input_paths:
for i, options in enumerate(kernel_filters):
with Image.load(input_path) as image:
output_path = f"{input_path}-{i}.png"
if aspycore.is_assignable(image, RasterImage):
raster = aspycore.as_of(image, RasterImage)
filter(raster, options, output_path)
outputs.append(output_path)
elif aspycore.is_assignable(image, VectorImage):
vector = aspycore.as_of(image, VectorImage)
vector_as_png = input_path + ".png"
if not os.path.exists(vector_as_png):
vector.save(vector_as_png)
outputs.append(vector_as_png)
with Image.load(vector_as_png) as png:
filter(aspycore.as_of(png, RasterImage), options, output_path)
outputs.append(output_path)
# Removing all output files
for p in outputs:
delete_file(p)