Python PIL Kernel() Method
Python PIL Kernel() Method
PIL is the Python Imaging Library, which provides image editing capabilities for the Python interpreter. The ImageFilter module contains definitions for a set of predefined filters that can be used with the Image.filter() method.
PIL.ImageFilter.Kernel() creates a convolution kernel. The current version only supports 3×3 and 5×5 integer and floating-point kernels.
Syntax: PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0)
Parameters:
size – Kernel size, given as (width, height). In the current version, this must be (3, 3) or (5, 5).
kernel – A sequence containing kernel weights.
scale – Scale factor. If given, the result for each pixel is divided by this value. Defaults to the sum of the kernel weights.
offset – Offset. If given, this value is added to the result after it is divided by the scale factor.
Return Type : An image.
Images used:
# Importing Image and ImageFilter module from PIL package
from PIL import Image, ImageFilter
#creating an image object
im1 = Image.open(r"C:UsersSystem-PcDesktoleave.JPG")
# applying the Kernel filter
im2 = im1.filter(ImageFilter.Kernel((3, 3),
(-1, -1, -1, -1, 9, -1, -1, -1, -1), 1, 0))
im2 = im2.show()
Output:
Another example: Here we change the kernel value to get the output. We can also change other parameters.
# Importing Image and ImageFilter module from PIL package
from PIL import Image, ImageFilter
# Importing Image and ImageFilter module from PIL package
from PIL import Image, ImageFilter
#creating an image object
im1 = Image.open(r"C:UsersSystem-PcDesktoleave.JPG")
# applying the Kernel filter
im2 = im1.filter(ImageFilter.Kernel((3, 3),
(-1, -1, -1, -1, 11, -2, -2, -2, -2), 1, 0))
im2 = im2.show()
Output: