PIL 图像上的羽状边缘
Feathered edges on image with PIL
如何使用枕头创建羽化边缘效果?
我所说的羽化边缘的意思是边缘被柔化,随着背景逐渐消失,看起来更像下图:
我尝试使用以下方法进行模糊处理:
im.filter(ImageFilter.BLUR)
但边缘保持锋利。
我不确定你想要的是模糊图像。我的印象是你所描述的是图像在图像边缘变得越来越透明。您可以通过创建和操作 alpha 通道来调整透明度。由于您使用的是 Python,下面是一个使用 Python、numpy 和来自 scikit-image 的宇航员图像的示例。 Alpha 通道被定义为中心恒定(无透明度),边缘为零(透明),具有线性渐变 in-between。您可以调整 alpha 通道,以在不透明和完全透明之间实现更平滑或更清晰的过渡。
import numpy as np
from skimage import data
astro = data.astronaut()
l_row, l_col, nb_channel = astro.shape
rows, cols = np.mgrid[:l_row, :l_col]
radius = np.sqrt((rows - l_row/2)**2 + (cols - l_col/2)**2)
alpha_channel = np.zeros((l_row, l_col))
r_min, r_max = 1./3 * radius.max(), 0.8 * radius.max()
alpha_channel[radius < r_min] = 1
alpha_channel[radius > r_max] = 0
gradient_zone = np.logical_and(radius >= r_min, radius <= r_max)
alpha_channel[gradient_zone] = (r_max - radius[gradient_zone])/(r_max - r_min)
alpha_channel *= 255
feathered = np.empty((l_row, l_col, nb_channel + 1), dtype=np.uint8)
feathered[..., :3] = astro[:]
feathered[..., -1] = alpha_channel[:]
import matplotlib.pyplot as plt
plt.imshow(feathered)
plt.show()
如何使用枕头创建羽化边缘效果?
我所说的羽化边缘的意思是边缘被柔化,随着背景逐渐消失,看起来更像下图:
我尝试使用以下方法进行模糊处理:
im.filter(ImageFilter.BLUR)
但边缘保持锋利。
我不确定你想要的是模糊图像。我的印象是你所描述的是图像在图像边缘变得越来越透明。您可以通过创建和操作 alpha 通道来调整透明度。由于您使用的是 Python,下面是一个使用 Python、numpy 和来自 scikit-image 的宇航员图像的示例。 Alpha 通道被定义为中心恒定(无透明度),边缘为零(透明),具有线性渐变 in-between。您可以调整 alpha 通道,以在不透明和完全透明之间实现更平滑或更清晰的过渡。
import numpy as np
from skimage import data
astro = data.astronaut()
l_row, l_col, nb_channel = astro.shape
rows, cols = np.mgrid[:l_row, :l_col]
radius = np.sqrt((rows - l_row/2)**2 + (cols - l_col/2)**2)
alpha_channel = np.zeros((l_row, l_col))
r_min, r_max = 1./3 * radius.max(), 0.8 * radius.max()
alpha_channel[radius < r_min] = 1
alpha_channel[radius > r_max] = 0
gradient_zone = np.logical_and(radius >= r_min, radius <= r_max)
alpha_channel[gradient_zone] = (r_max - radius[gradient_zone])/(r_max - r_min)
alpha_channel *= 255
feathered = np.empty((l_row, l_col, nb_channel + 1), dtype=np.uint8)
feathered[..., :3] = astro[:]
feathered[..., -1] = alpha_channel[:]
import matplotlib.pyplot as plt
plt.imshow(feathered)
plt.show()