用python画圆角矩形_如何使用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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值