- PIL Home
- Python Image Library Intro
- Python Image Library Setup
- Python Image Library Basics
- Python Image Library Hierarchy
- Python Image Library Modules
- Python ImageColor Module
- Python ImageDraw Module
- Python ImageStat Module
- Python ImageTK Module
- Python ImageFile Module
- Python ImageFilter Module
- Python ImageGrab Module
- Python ImageMath Module
- Python - Loading Image
- Python - Rotating Image
- Python - Blurring Image
- Python - Converting Image
- Python - Cropping Image
- Python - Image Thumbnails
- Python - Text on Images
- Python - Multiple Image Copies
- Python - Saving Image As PDF
- Python - Saving Screenshot
- Python - Resizing Image
- Python - Adding Watermark
- Python Image Library Resources
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Blurring Image
Description
Blurring the image means making the image unfocussed. In general terms, blurring is generally obtained by convolving the image with a low pass filter.Blurring the image is very useful for generating background effects and shadows.
For blurring the image we need to import Image and ImageFilter modules. Image is the fundamental module for loading and opening the image and ImageFilter module provides various predefined image enhancement filters.
Let us take the below image and will try to blur the image.
First import the Image module and ImageFilter modules and then open the image with open method in Image.ImageFilter contains the various attributes that are used to blue the image.Make sure to give the complete path of the image where it has been placed.
from PIL import Image,ImageFilter
flowers = Image.open("flowers.jpg")
flowers = Image.open("flowers.jpg")
Apply the ImageFilter attributes as below.
im = im.filter(ImageFilter.BLUR)
Save the blurred file with save method from Image module.
im.save("afterblur_new.jpg")
Gaussian Blur
A Gaussian blur (also known as Gaussian smoothing) is the result of blurring an image by a Gaussian function. It is a widely used effect in graphics software, typically to reduce image noise and reduce detail.
In order to get the Gaussian blur effect, replace ImageFilter.BLURwith ImageFilter.GaussianBlur
Difference between Normal Blur and Gaussian Blur
The central difference, depending on algorithm, is that Gaussian blur takes a weighted average around the pixel, while "normal" blur just averages all the pixels in the radius of the single pixel together.
Syntax
Following is the syntax of functions been used.
# Import Pillow:
from PIL import Image,ImageFilter
# Load the original image:
object = Image.open(image)
object = object.filter(type of blur)
object.save(image)
from PIL import Image,ImageFilter
# Load the original image:
object = Image.open(image)
object = object.filter(type of blur)
object.save(image)
Parameters
For the filter method we will pass the type of blur from ImageFilter library.Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directory, you are supposed to define the complete path of the image.
Return value
The new image which has been blurred will be saved to the local directory with the save method.Examples
The following example shows how to blur the image in various styles.
from PIL import Image,ImageFilter
im = Image.open("images11.jpg")
im = im.filter(ImageFilter.BLUR)
im.save("afterblur_new.jpg")
im = Image.open("images11.jpg")
im = im.filter(ImageFilter.BLUR)
im.save("afterblur_new.jpg")
Before
After
Gaussian blur example
from PIL import Image,ImageFilter
im = Image.open("images11.jpg")
im = im.filter(ImageFilter.GaussianBlur)
im.save("afterblur_new.jpg")
im = Image.open("images11.jpg")
im = im.filter(ImageFilter.GaussianBlur)
im.save("afterblur_new.jpg")
Normal Blur
Gaussian Blur
Advertisements