【问题标题】:How to blur an ellipse with ImageDraw如何使用 ImageDraw 模糊椭圆
【发布时间】:2019-04-11 09:53:31
【问题描述】:

我实现了一种检测人脸的算法,我想对人脸进行模糊处理。我正在使用 PIL 来模糊它。

image = Image.open(path_img)
draw = ImageDraw.Draw(image)
draw.ellipse((top, left, bottom, right), fill = 'white', outline ='white')

我的代码得到了这个

Face to blur

我想使用:

blurred_image = cropped_image.filter(ImageFilter.GaussianBlur(radius=10 ))

但我不能使用它,因为我使用的是 ImageDraw 并且它仅适用于 Image 类。如何用椭圆(圆形)模糊脸部?

谢谢

【问题讨论】:

  • 您要模糊脸部,还是模糊现在覆盖脸部的椭圆?
  • 两者都对我有好处,但我认为模糊椭圆是最好的方法
  • 您应该考虑通过单击选票旁边的空心对勾(复选标记)来接受您喜欢的答案,这样它就会变成绿色,并且作者会获得积分奖励,未来的读者会知道您的想法您的问题的解决方案。投票有用的答案还可以鼓励人们回答您的问题,并且不会花费您任何费用。谢谢。

标签: python python-imaging-library imagefilter


【解决方案1】:

模糊椭圆是最好的方法

使用 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))

为我们提供了这种模糊变体(模糊的边缘平滑:))


【讨论】:

    【解决方案2】:

    不确定您是想在图像上合成一些东西以隐藏内容还是模糊它。这更模糊:-)

    从帕丁顿开始:

    你可以像这样进入“隐身模式”

    #!/usr/bin/env python3
    
    from PIL import Image, ImageDraw, ImageFilter
    import numpy as np
    
    # Open image
    im = Image.open('paddington.png')
    
    # Make a mask the same size as the image filled with black
    mask = Image.new('RGB',im.size)
    
    # Draw a filled white circle onto the black mask
    draw = ImageDraw.Draw(mask)
    draw.ellipse([90,40,300,250],fill=(255,255,255))
    
    # Blur the entire image
    blurred = im.filter(ImageFilter.GaussianBlur(radius=15))
    
    # Select either the original or the blurred image at each pixel, depending on the mask
    res = np.where(np.array(mask)>0,np.array(blurred),np.array(im)) 
    
    # Convert back to PIL Image and save
    Image.fromarray(res).save('result.png')
    


    或者,正如@AKX 所建议的,您可以删除 Numpy 依赖项并使代码也更小一些,但仍然得到相同的结果:

    #!/usr/bin/env python3
    
    from PIL import Image, ImageDraw, ImageFilter
    import numpy as np
    
    # Open image
    im = Image.open('paddington.png')
    
    # Make a mask the same size as the image filled with black
    mask = Image.new('L',im.size)
    
    # Draw a filled white circle onto the black mask
    draw = ImageDraw.Draw(mask)
    draw.ellipse([90,40,300,250],fill=255)
    
    # Blur the entire image
    blurred = im.filter(ImageFilter.GaussianBlur(radius=15))
    
    # Composite blurred image over sharp one within mask
    res = Image.composite(blurred, im, mask)
    
    # Save
    res.save('result.png')
    

    【讨论】:

    • 使用 Image.composite 会放弃 numpy 依赖。 :)
    • @AKX 我从来没有遇到过要求Numpy 的问题,因为它无处不在,但我会尝试一下,看看我是否可以按照您的建议进行操作。谢谢。
    • 谢谢!它解决了我的问题,你太棒了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2017-06-27
    相关资源
    最近更新 更多