Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feathered edges on image with Pillow

I'm trying to figure out how to feather the edges of an image using Pillow with Python.

I need something like this cute cat (ignore the visible edges):

enter image description here

I tried im.filter(ImageFilter.BLUR) but is not what I'm looking for.

like image 678
exsnake Avatar asked Oct 30 '25 19:10

exsnake


1 Answers

Have a look at this example:

from PIL import Image
from PIL import ImageFilter

RADIUS = 10

# Open an image
im = Image.open(INPUT_IMAGE_FILENAME)

# Paste image on white background
diam = 2*RADIUS
back = Image.new('RGB', (im.size[0]+diam, im.size[1]+diam), (255,255,255))
back.paste(im, (RADIUS, RADIUS))

# Create blur mask
mask = Image.new('L', (im.size[0]+diam, im.size[1]+diam), 255)
blck = Image.new('L', (im.size[0]-diam, im.size[1]-diam), 0)
mask.paste(blck, (diam, diam)) 

# Blur image and paste blurred edge according to mask
blur = back.filter(ImageFilter.GaussianBlur(RADIUS/2))
back.paste(blur, mask=mask)
back.save(OUTPUT_IMAGE_FILENAME)

Original image (author - Irene Mei):

Kitty

Pasted on white background:

Kitty pasted on white background

Blur region (paste mask):

Blur region

Result:

Image of a kitty with feathered (blurred) edges

like image 53
Andriy Makukha Avatar answered Nov 01 '25 15:11

Andriy Makukha