Created
March 16, 2020 05:35
-
-
Save namieluss/2a57539df54158e0c1575903a988bbb2 to your computer and use it in GitHub Desktop.
Blur Image borders using Python Pillow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Thanks for sharing
You're welcome.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing