Pillow 图像上的羽状边缘

Feathered edges on image with Pillow

我正在尝试弄清楚如何使用带有 Python 的 Pillow 羽化图像的边缘。

我需要像这只可爱的猫(忽略可见边缘):

我试过 im.filter(ImageFilter.BLUR) 但不是我要找的东西。

看看这个例子:

from PIL import Image
from PIL import ImageFilter

RADIUS = 10

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

# Paste image on white background
diam = 2*RADIUS
back = Image.new('RGB', (im.size[0]+diam, im.size[1]+diam), (255,255,255))
back.paste(im, (RADIUS, RADIUS))

# Create blur mask
mask = Image.new('L', (im.size[0]+diam, im.size[1]+diam), 255)
blck = Image.new('L', (im.size[0]-diam, im.size[1]-diam), 0)
mask.paste(blck, (diam, diam)) 

# Blur image and paste blurred edge according to mask
blur = back.filter(ImageFilter.GaussianBlur(RADIUS/2))
back.paste(blur, mask=mask)
back.save(OUTPUT_IMAGE_FILENAME)

原图(作者 - Irene Mei):

粘贴在白色背景上:

模糊区域(粘贴蒙版):

结果:

提供带有渐变粘贴蒙版的修改解决方案(根据@Crickets 的要求)。

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

RADIUS = 10

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

# Paste image on white background
diam = 2*RADIUS
back = Image.new('RGB', (im.size[0]+diam, im.size[1]+diam), (255,255,255))
back.paste(im, (RADIUS, RADIUS))

# Create paste mask
mask = Image.new('L', back.size, 0)
draw = ImageDraw.Draw(mask)
x0, y0 = 0, 0
x1, y1 = back.size
for d in range(diam+RADIUS):
    x1, y1 = x1-1, y1-1
    alpha = 255 if d<RADIUS else int(255*(diam+RADIUS-d)/diam)
    draw.rectangle([x0, y0, x1, y1], outline=alpha)
    x0, y0 = x0+1, y0+1

# Blur image and paste blurred edge according to mask
blur = back.filter(ImageFilter.GaussianBlur(RADIUS/2))
back.paste(blur, mask=mask)
back.save(OUTPUT_IMAGE_FILENAME)

粘贴蒙版:

结果: