Skip to content Skip to sidebar Skip to footer

How To Blur Non-rectangular Or Circular Area Of Image With Python Pil?

Using PIL in Python, I am superimposing a PNG image on top of another, larger image. The smaller image is semi-transparent. I would like for the area behind the smaller image to be

Solution 1:

Take a look at this example (rounded_rectangle function from here):

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFilter

defrounded_rectangle(draw, xy, rad, fill=None):
    x0, y0, x1, y1 = xy
    draw.rectangle([ (x0, y0 + rad), (x1, y1 - rad) ], fill=fill)
    draw.rectangle([ (x0 + rad, y0), (x1 - rad, y1) ], fill=fill)
    draw.pieslice([ (x0, y0), (x0 + rad * 2, y0 + rad * 2) ], 180, 270, fill=fill)
    draw.pieslice([ (x1 - rad * 2, y1 - rad * 2), (x1, y1) ], 0, 90, fill=fill)
    draw.pieslice([ (x0, y1 - rad * 2), (x0 + rad * 2, y1) ], 90, 180, fill=fill)
    draw.pieslice([ (x1 - rad * 2, y0), (x1, y0 + rad * 2) ], 270, 360, fill=fill)

# Open an image
im = Image.open(INPUT_IMAGE_FILENAME)

# Create rounded rectangle mask
mask = Image.new('L', im.size, 0)
draw = ImageDraw.Draw(mask)
rounded_rectangle(draw, (im.size[0]//4, im.size[1]//4, im.size[0]//4*3, im.size[1]//4*3), rad=40, fill=255)
mask.save('mask.png')

# Blur image
blurred = im.filter(ImageFilter.GaussianBlur(20))

# Paste blurred region and save result
im.paste(blurred, mask=mask)
im.save(OUTPUT_IMAGE_FILENAME)

Input image:

Can of Coke on a beach (in Ukraine)

Mask:

White rectangle with rounded corners on a black background

Output image:

Blurred can of Coke on a beach

Tested with Python 2.7.12 and Pillow 3.1.2 (it doesn't have BoxBlur).

Post a Comment for "How To Blur Non-rectangular Or Circular Area Of Image With Python Pil?"