简体   繁体   中英

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 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.

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:

可乐在海滩上(在乌克兰)

Mask:

与圆角落的白色长方形在黑背景

Output image:

被弄脏的罐头在海滩的可乐

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM