如何使用ImageDraw模糊椭圆

时间:2019-04-11 09:53:32

标签: python python-imaging-library imagefilter

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

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

我用我的代码

Face to blur

我想使用:

require(lubridate)
require(tidyverse)

## Create some sample data: 
time_index <- seq(from = as.POSIXct("2017-01-01 07:00"), 
                  to = as.POSIXct("2018-01-01 18:00"), by = "hour")
value <- rnorm(n = length(time_index))
data <- data.frame(time_index,value)


data <- data %>% mutate (hour = hour(time_index),
                         month = month(time_index)) %>%
  group_by(month,hour) 

head(data)
> data
# A tibble: 8,772 x 4
# Groups:   month, hour [288]
   time_index           value  hour month
   <dttm>               <dbl> <int> <dbl>
 1 2017-01-01 07:00:00 -0.626     7     1
 2 2017-01-01 08:00:00  0.184     8     1
 3 2017-01-01 09:00:00 -0.836     9     1
 4 2017-01-01 10:00:00  1.60     10     1
 5 2017-01-01 11:00:00  0.330    11     1
 6 2017-01-01 12:00:00 -0.820    12     1
 7 2017-01-01 13:00:00  0.487    13     1
 8 2017-01-01 14:00:00  0.738    14     1
 9 2017-01-01 15:00:00  0.576    15     1
10 2017-01-01 16:00:00 -0.305    16     1
# ... with 8,762 more rows

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

谢谢

2 个答案:

答案 0 :(得分:1)

  

模糊椭圆是最好的方法

使用枕头,最好的方法是使用模糊的椭圆作为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作为输入,输出为

a censored kitten


编辑:受马克·塞彻尔(Mark Setchell)的回答启发,只需将overlay_image行更改为

overlay_image = kitten_image.filter(ImageFilter.GaussianBlur(radius=15))

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

enter image description here


答案 1 :(得分:0)

不确定是否要在图像上合成某些东西以隐藏内容或使其模糊。这是更加模糊的:-)

从帕丁顿开始:

enter image description here

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

#!/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')

enter image description here


或者,如@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')