用枕头在图像上羽化边缘 [英] Feathered edges on image with Pillow

查看:151
本文介绍了用枕头在图像上羽化边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使用带Pillow和Python的羽毛图像边缘.

I'm trying to figure out how to feather the edges of an image using Pillow with Python.

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

I need something like this cute cat (ignore the visible edges):

我尝试了im.filter(ImageFilter.BLUR),但不是我想要的.

I tried im.filter(ImageFilter.BLUR) but is not what I'm looking for.

推荐答案

看看这个例子:

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 ):

粘贴在白色背景上:

模糊区域(粘贴蒙版):

Blur region (paste mask):

结果:

这篇关于用枕头在图像上羽化边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆