Mumfordwiz
Mumfordwiz

Reputation: 1545

blurring an image using PIL in python

I have been trying to blur an image using the PIL.

from what I know i need to copy the image, and then change every pixel to the average of the pixels surrounding him, from the original picture. so I didn't get really far, i'm using python 3.3x

from PIL import Image 

img = Image.open("source")
im = Image.copy(img)

I know how to use putpixe, and get a pixel's data, but I can't figure out how to get the average of the pixels around.

Thanks in advance for the help!

Upvotes: 22

Views: 46131

Answers (1)

tom10
tom10

Reputation: 69172

You can just do:

blurred_image = original_image.filter(ImageFilter.BLUR)

See the ImageFilter module for more options.

You are correct in that the process you describe would blur the image, and there are filters that essentially directly do what you suggest (*e.g.", using the ImageFilter.Kernel method where you kernel has constant weights). Using ImageFilter will be faster and easier though, and give you more options for blurring and beyond.

Upvotes: 35

Related Questions