How to blur non-rectangular or circular area of image with Python PIL?

Crickets picture Crickets · Jun 11, 2018 · Viewed 8.1k times · Source

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 blurred on the larger image. The following code blurs a rectangular area:

box = (3270, 1150, 4030, 2250)      # (x1, y1, x2, y2)
ic = outputImage.crop(box)
ic = ic.filter(ImageFilter.BoxBlur(20))
outputImage.paste(ic, box)

However, I need to blur a rectangular area that has rounded corners.

This is what the superimposed image looks like:

So, is it possible to define a custom shape for a cropped area in PIL?

If not, is it possible to at least crop circle-shaped areas? (For full coverage and without any overhang, my area would have to broken down into 6 sub-areas: 4 circles and 2 rectangles. Doing all this will slow down my code, but I will take whatever solution I can get.)

I understand that this can be done with Numpy, but I would prefer to use PIL because everything else in this script is already coded with PIL.

Answer

Andriy Makukha picture Andriy Makukha · Jun 12, 2018

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

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

def rounded_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).