我实现了一种检测人脸的算法,我想对人脸进行模糊处理。我正在使用 PIL 来模糊它。
image = Image.open(path_img)
draw = ImageDraw.Draw(image)
draw.ellipse((top, left, bottom, right), fill = 'white', outline ='white')
我用我的代码得到了这个
我想使用:
blurred_image = cropped_image.filter(ImageFilter.GaussianBlur(radius=10 ))
但我不能使用它,因为我使用的是 ImageDraw 并且它只适用于 Image 类。如何用椭圆(圆形)模糊脸部?
谢谢
请您参考如下方法:
blur the ellipse is the best way
使用 Pillow,最好的方法是使用模糊椭圆作为 composite 的混合蒙版。
from PIL import Image, ImageDraw, ImageFilter
def make_ellipse_mask(size, x0, y0, x1, y1, blur_radius):
img = Image.new("L", size, color=0)
draw = ImageDraw.Draw(img)
draw.ellipse((x0, y0, x1, y1), fill=255)
return img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
kitten_image = Image.open("kitten.jpg")
overlay_image = Image.new("RGB", kitten_image.size, color="orange") # This could be a bitmap fill too, but let's just make it orange
mask_image = make_ellipse_mask(kitten_image.size, 150, 70, 350, 250, 5)
masked_image = Image.composite(overlay_image, kitten_image, mask_image)
masked_image.show()
给定this adorable kitten作为输入,输出是
编辑: 受 Mark Setchell 的回答启发,只需将 overlay_image 行更改为
overlay_image = kitten_image.filter(ImageFilter.GaussianBlur(radius=15))
给我们这个模糊变体(模糊的边缘平滑 :))
