English 中文(简体)
Python Pillow - Adding Filters to an Image
  • 时间:2024-12-22

Python Pillow - Adding Filters to an Image


Previous Page Next Page  

The ImageFilter module contains definitions for a pre-defined set of filters, which we used with Image.filter() method. These filters are used to change the looks and feel of the image.

Example

Below example is Filtering an image −

from PIL import Image, ImageFilter

im = Image.open( jungleSaf2.jpg )

im1 = im.filter(ImageFilter.BLUR)
im1.show()

im2 = im.filter(ImageFilter.MinFilter(3))
im2.show()

im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3)
im3.show()

In above program, we have used the MinFilter() method, which is used to create a minimum filter. It picks the lowest pixel value in a window with the given size.

ImageFilter.MinFilter(size=3)

Where,

size − The kernel size, in pixels.

Output

If you save the above program and execute, it shows the original image, blurred image and, the blurred image with MinFilter using standard PNG display utipty, as follows −

Original Image

Original Images

Blurred Image

Blurred Image6

Image blurred with mini filter

Image Blurred With Mini Filter

Filters

The current version of pillow pbrary provides below mentioned set of predefined image enhancement filters.

    BLUR

    CONTOUR

    DETAIL

    EDGE_ENHANCE

    EDGE_ENHANCE_MORE

    EMBOSS

    FIND_EDGES

    SHARPEN

    SMOOTH

    SMOOTH_MORE

Example

Following python example apppes the blur filter on an image saves it and, displays it using standard PNG display utipty −

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow

from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)

#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(BLUR)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

Image Filter

In the same way, to the image.filter() method you can pass any of the following parameters to get respective outputs −

    CONTOUR

    DETAIL

    EDGE_ENHANCE

    EDGE_ENHANCE_MORE

    EMBOSS

    FIND_EDGES

    SMOOTH

    SMOOTH_MORE

    SHARPEN

Python img.filter(CONTOUR) method

Following python example apppes CONTOUR filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(CONTOUR)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image

Python img.filter(DETAIL) method

Following python example apppes DETAIL filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(DETAIL)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image, and the filtered image using standard PNG display utipty, as follows −

Original image

Original image

Filtered image

Filtered Image1

Python img.filter(EDGE_ENHANCE) method

Following python example apppes EDGE_ENHANCE filter to the given image −

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(EDGE_ENHANCE)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image, and the filtered image using standard PNG display utipty, as follows −

Original image

Orignal Image

Filtered image

Filtered Image2

Python img.filter(EDGE_ENHANCE_MORE) method

Following python example apppes EDGE_ENHANCE_MORE filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(EDGE_ENHANCE_MORE)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image, and the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image3

Python img.filter(EMBOSS) method

Following python example apppes EMBOSS filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(EMBOSS)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image4

Python img.filter(FIND_EDGES) method

Following python example apppes FIND_EDGES filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(FIND_EDGES)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image5

Python img.filter(SMOOTH) method

Following python example apppes SMOOTH filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(SMOOTH)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image6

Python img.filter(SHARPEN) method

Following python example apppes SHARPEN filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter

#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(SHARPEN)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image7

Following python example apppes SHARPEN filter to the given image.

Example

#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
   BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
   EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open( images/cat.jpg )
#Applying the blur filter
img1 = img.filter(SHARPEN)
img1.save( images/ImageFilter_blur.jpg )
img1.show()

Output

If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −

Original image

Original Image

Filtered image

Filtered Image8 Advertisements