Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Python Pillow - Convolution Filters



    In the context of image processing, Convolution involves applying a small matrix (known as convolution kernel) of values to an image. This process results in various filtering effects such as blurring, sharpening, embossing, and edge detection. Each value in the kernel represents a weight or coefficient. This kernel is applied to a corresponding neighborhood of pixels in the image to produce the output pixel value at the corresponding position in the output image.

    Python's Pillow library provides a specific class known as "kernel" within its ImageFilter module. This class is used to create convolution kernels of sizes that extend beyond the conventional 5x5 matrices.

    Creating the Convolution kernel

    To create a convolution kernel you can use the Kernel() class from the ImageFilter module.

    It's important to note that the current version of Pillow supports 33 and 55 integer and floating-point kernels. And these kernels apply exclusively to images in "L" and "RGB" modes.

    Following is the syntax of this ImageFilter.Kernel() class −

    class PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0)
    

    Here are the details of the class parameters −

    • size − Represents the kernel size, specified as (width, height). In the current version, the valid sizes are (3,3) or (5,5).

    • kernel − A sequence containing the kernel weights. The kernel is vertically flipped before applying it to the image.

    • scale − Denotes the scale factor. If provided, the result for each pixel is divided by this value. The default value is the sum of the kernel weights.

    • offset − Signifies an offset value. If provided, this value is added to the result after being divided by the scale factor.

    Example - Applying a Convolution Kernel Filter

    This example demonstrates how to apply a convolution kernel filter to an image using the Image.filter() method.

    main.py

    from PIL import Image, ImageFilter
    
    # Create an image object
    original_image = Image.open('Images/yellow_car.jpg')
    
    # Apply the Kernel filter
    filtered_image = original_image.filter(ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 5, -1, 0, -1, 0)))
    
    # Display the original image 
    original_image.show()
    
    # Display the filtered image
    filtered_image.show()
    

    Input Image

    yellow car

    Output

    Output of the convolution kernel filter −

    convolution kernel filter

    Example - Applying an Emboss Convolution Filter

    Here's an example of applying a 5x5 emboss convolution kernel filter to an image.

    main.py

    from PIL import Image, ImageFilter
    
    # Create an image object
    original_image = Image.open('Images/yellow_car.jpg')
    
    # Define a 5x5 convolution kernel
    kernel_5x5 = [-2,  0, -1,  0,  0,
       0, -2, -1,  0,  0,
       -1, -1,  1,  1,  1,
       0,  0,  1,  2,  0,
       0,  0,  1,  0,  2]
    
    # Apply the 5x5 convolution kernel filter
    filtered_image = original_image.filter(ImageFilter.Kernel((5, 5), kernel_5x5, 1, 0))
    
    # Display the original image 
    original_image.show()
    
    # Display the filtered image
    filtered_image.show()
    

    Input Image

    yellow car

    Output Image

    Output of the convolution kernel filter of size 5X5 −

    imagefilter kernel
    Advertisements