The Wayback Machine - https://web.archive.org/web/20240828171152/https://www.geeksforgeeks.org/python-pil-kernel-method/
Open In App

Python PIL | Kernel() method

Last Updated : 14 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:



Similar Reads

Stop the Jupyter Kernel if Kernel is not responding
Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Google Colab and Kaggle Notebook are inspired by it. Data transformation and cleaning, statistical modelling, data visualization, machine learning, and many other applications are ju
7 min read
Python PIL | GaussianBlur() method
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.GaussianBlur() method create Gaussian blur filter. Syntax: PIL.ImageFilter.GaussianBlur(radius=5) Par
1 min read
Python PIL | BoxBlur() method
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.BoxBlur() Blurs the image by setting each pixel to the average value of the pixels in a square box ext
2 min read
Python PIL | getbands() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.getbands() method returns a tuple containing the name of each band in the image.For example, getbands on an RGB image returns (“R”, “G”, “B”). Syntax: PIL.Image.getbands() Parameters: no arguments Returns: A tuple containing band names
1 min read
Python PIL | logical_and() and logical_or() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Python PIL | ImageChops.subtract() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Python PIL | UnsharpMask() method
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.UnsharpMask() method applies the Unsharp mask filter to the input image. Syntax: PIl.ImageFilter.Unsha
2 min read
Python PIL | ImageChops.subtract() and ImageChops.subtract_modulo() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. ImageChops.subtract() method - This method subtracts two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0. Syntax: ImageChops.subtract(image1, image2, scale = 1.0, offset = 0) Param
2 min read
Python PIL | Image.crop() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image. Syntax: PIL.Image.crop(box = None)Parameters: box - a 4-tuple defining the left, upper, right, and lower pixel coordinate.Return type: Image (Returns a rectangular regio
2 min read
Python PIL | ImageOps.expand() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. the ImageOps module contains a number of ‘ready-made’ image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images.ImageOps.expand() adds a border to the image upon which this function is ca
1 min read
Python PIL | ImageEnhance.Color() and ImageEnhance.Contrast() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageEnhance module contains a number of classes that can be used for image enhancement. ImageEnhance.Color() method - This class can be used to adjust the color balance of an image, in a manner similar to the controls on a color TV set. An
2 min read
Python PIL | ImageGrab.grabclipboard() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory. PIL.ImageGrab.grabclipboard() method takes a snapshot of the clipboard image, if any. Syntax: PIL.ImageGrab.grabclipboard() Parameters: n
1 min read
Python PIL | RankFilter() method
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.RankFilter() creates a rank filter. The rank filter sorts all pixels in a window of the given size, a
2 min read
Python PIL | ImageChops.screen() and ImageChops.offset() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. ImageChops.screen() method - This method is used to superimpose two inverted images on top of each other. Syntax: ImageChops.screen(image1, image2) Parameters: image1 first image image2 second image Return Value: An Image C/C++ Code # This will
2 min read
Python PIL | MedianFilter() and ModeFilter() method
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.MedianFilter() method creates a median filter. Picks the median pixel value in a window with the give
1 min read
Python PIL | Image.new() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.new() method creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels. The color is given as a single value for single-band images, and a tuple for multi-band images (with one value for each
2 min read
Python PIL | paste() and rotate() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.Image.paste() method is used to paste an image on another image. This is where the new() method comes in handy. Syntax: PIL.Image.Image.paste(image_1, image_2, box=None, mask=None) OR image_object.paste(image_2, box=None, mask=None)Par
2 min read
Python PIL | ImageChops.darker() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. ImageChops.darker() method is used to compare two images pixel by pixel, and returns a new image containing the darker values. Syntax: ImageChops.darker(image1, image2) Parameters: image1: It is the image object or image path of first image. ima
1 min read
Python PIL | Image.split() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. Image.split() method is used to split the image into individual bands. This method returns a tuple of individual image bands from an image. Splitting an “RGB” image creates three new images each containing a copy of one of the original bands (re
1 min read
Python PIL | ImageChops.add() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
2 min read
Python PIL | copy() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.copy() method copies the image to another image object, this method is useful when we need to copy the image but also retain the original. Syntax:Image.copy() Parameters: no arguments Returns:An image object # Importing Image module fr
1 min read
Python PIL | composite() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.composite() method creates composite image by blending images using a transparency mask. Here, mask is another image which remains transparent when composite together. Syntax: PIL.Image.composite(image1, image2, mask) Parameters: image
2 min read
Python PIL | ImageChops.multiply() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Python PIL | ImageChops.lighter() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Python PIL | blend() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.blend() method creates a new image by interpolating between two input images, using a constant alpha. Syntax: PIL.Image.blend(image1, image2, alpha). Parameter: image1: first image image2: second image, must have the same mode and size
2 min read
Python PIL | MinFilter() and MaxFilter() method
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.MinFilter() method creates a min filter. Picks the lowest pixel value in a window with the given size
1 min read
Python PIL | ImageGrab.grab() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory.PIL.ImageGrab.grab() method takes a snapshot of the screen. The pixels inside the bounding box are returned as an “RGB” image on Windows o
1 min read
Python PIL | ImageChops.add_modulo() and ImageChops.difference() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. ImageChops.add_modulo() method - This method is used to add two images without clipping them unlike ImageChops.add() which clips the images. Syntax: ImageChops.add_modulo(image1, image2) Parameters: image1 is the first image image2 is the second
1 min read
Python PIL | ImageEnhance.Brightness() and ImageEnhance.Sharpness() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageEnhance module contains a number of classes that can be used for image enhancement. ImageEnhance.Brightness() method - This class can be used to control the brightness of an image. An enhancement factor of 0.0 gives a black image. A fac
2 min read
Python PIL | logical_xor() and invert() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Article Tags :
Practice Tags :