How to blur an ellipse with ImageDraw

Asked by At

I implemented an algorithm that detects faces and I want to blur faces. I'm using PIL for blurring it.

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

I got this with my code

Face to blur

I would like to use :

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

But I can't use it because I'm using an ImageDraw and it works only with Image class. How can I blur with an ellipse (circular) the face?

Thank you

2 Answers

1
AKX On

blur the ellipse is the best way

With Pillow, the best way to do this is to use the blurred ellipse as a blending mask for 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()

Given this adorable kitten as input, the output is

a censored kitten


EDIT: Inspired by Mark Setchell's answer, simply changing the overlay_image line to

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

gives us this blur variant (with smooth edges for the blur :) )

enter image description here


0
Mark Setchell On

Not sure if you want to composite something over the image to conceal the contents, or blur it. This is more blurry :-)

Starting with Paddington:

enter image description here

You can go to "Stealth Mode" like this:

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


Or, as suggested by @AKX, you can remove the Numpy dependency and make the code a bit smaller too yet still get same result:

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