PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method.
PIL.ImageFilter.Kernel() Create 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. the default is the sum of the kernel weights.
offset – Offset. If given, this value is added to the result, after it has been divided by the scale factor.Returns type: An image.
Image Used:

# Importing Image and ImageFilter module from PIL package from PIL import Image, ImageFilter # creating a image object im1 = Image.open(r"C:\Users\System-Pc\Desktop\leave.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 change kernel value to obtain output, we can change other parameters as well.
# 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 a image object im1 = Image.open(r"C:\Users\System-Pc\Desktop\leave.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:

Recommended Posts:
- Creating linear kernel SVM in Python
- Major Kernel Functions in Support Vector Machine (SVM)
- Python PIL | GaussianBlur() method
- Python PIL | BoxBlur() method
- Python PIL | getbands() method
- Python PIL | logical_and() and logical_or() method
- Python PIL | ImageChops.subtract() method
- Python PIL | UnsahrpMask() method
- Python PIL | ImageChops.subtract() and ImageChops.subtract_modulo() method
- Python PIL | Image.crop() method
- Python PIL | ImageOps.expand() method
- Python PIL | ImageEnhance.Color() and ImageEnhance.Contrast() method
- Python PIL | ImageGrab.grabclipboard() method
- Python PIL | RankFilter() method
- Python PIL | ImageChops.screen() and ImageChops.offset() method
- Python PIL | MedianFilter() and ModeFilter() method
- Python PIL | Image.new() method
- Python PIL | getbands() and getextrema() method
- Python PIL | paste() and rotate() method
- Python PIL | ImageChops.darker() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

