Blur Filters
Blur Box Kernel Filter in Python
Filling the kernel matrix with uniform elements and maintaining a cumulative sum of 1 allows for the calculation of the average value within the surrounding pixel area, utilizing a 5x5 matrix. The resulting pixel value is determined by summing the 1/25 values or 4% from all adjacent pixels. This specific kernel matrix is identified as a 'Blur Box.' The augmentation of matrix dimensions enhances the blurring effect proportionally.
# Blur Box 5x5 uniform kernel
[
[ 0.04, 0.04, 0.04, 0.04, 0.04,],
[ 0.04, 0.04, 0.04, 0.04, 0.04,],
[ 0.04, 0.04, 0.04, 0.04, 0.04,],
[ 0.04, 0.04, 0.04, 0.04, 0.04,],
[ 0.04, 0.04, 0.04, 0.04, 0.04,],
]
This process initiates a gradual change in pixel values among neighboring elements, effectively introducing a blur to the image. It assists in reducing noise, lowering sharpness, and yielding a soft, indistinct visual outcome.


Python code example
The provided Python code example demonstrates the utilization of the Aspose.Imaging Python API. Utilize the `ConvolutionFilter` class, which provides pre-defined kernel filters, including get_blur_box() with adjustable size settings. Furthermore, you retain the flexibility to craft your custom kernel matrix. In this example, image templates in raster PNG and vector SVG formats are loaded from the "templates" folder, and a set of filters are applied from a predefined 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) |