Skip to content

Instantly share code, notes, and snippets.

@shubhamoli
Last active April 12, 2020 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shubhamoli/05a82f5e543ad3065f3f50ff6109ec05 to your computer and use it in GitHub Desktop.
Save shubhamoli/05a82f5e543ad3065f3f50ff6109ec05 to your computer and use it in GitHub Desktop.
# Rounded Corners - Python's pillow v7.1.1
# Ref: https://stackoverflow.com/a/11291419/7416198
from PIL import Image, ImageDraw
# im Image() Image object
# rad int radius of curve
def add_corners(im, rad):
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new('L', im.size, 255)
w, h = im.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0)) # Top left
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad)) # Bottom-left
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0)) # Top Right
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad)) # Bottom Right
im.putalpha(alpha)
return im
im = Image.open('input.jpg')
im = add_corners(im, 100) # img.size[0]//2 for circular image (only square images)
im.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment