Skip to content

Instantly share code, notes, and snippets.

@namieluss
Created March 16, 2020 05:35
Show Gist options
  • Save namieluss/2a57539df54158e0c1575903a988bbb2 to your computer and use it in GitHub Desktop.
Save namieluss/2a57539df54158e0c1575903a988bbb2 to your computer and use it in GitHub Desktop.
Blur Image borders using Python Pillow
from PIL import Image, ImageFilter
# blur radius and diameter
radius, diameter = 20, 40
# open an image
img = Image.open("test_image.jpg")
# Paste image on white background
background_size = (img.size[0] + diameter, img.size[1] + diameter)
background = Image.new('RGB', background_size, (255, 255, 255))
background.paste(img, (radius, radius))
# create new images with white and black
mask_size = (img.size[0] + diameter, img.size[1] + diameter)
mask = Image.new('L', mask_size, 255)
black_size = (img.size[0] - diameter, img.size[1] - diameter)
black = Image.new('L', black_size, 0)
# create blur mask
mask.paste(black, (diameter, diameter))
# Blur image and paste blurred edge according to mask
blur = background.filter(ImageFilter.GaussianBlur(radius / 2))
background.paste(blur, mask=mask)
background.save("test_image_blurred.jpg", quality=100))
# show blurred edged image in preview
background.show()
@senemaktas
Copy link

Thanks for sharing

@namieluss
Copy link
Author

Thanks for sharing

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment