3
votes

I'm currently using this method to round the edges on images for my users:

def _add_corners(self, im, rad=100):
    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, "white")
    w, h = im.size
    alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
    alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
    alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
    alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
    im.putalpha(alpha)
    return im

The rounding works perfectly fine and i'm happy with it. However, I would like to also draw a border around the image within the constraints of the edges. Most of what I'm reading online shows how to draw border on the image itself (not the rounded border i'm doing). Is there a way to do that? I have read the below:

how to round_corner a logo without white background(transparent?) on it using pil?

Python Imaging Library (PIL) Drawing--Rounded rectangle with gradient

Any way to make nice antialiased round corners for images in python?

You could create a function that draws the four straight lines and the four corners separately, the latter one using the method PIL.ImageDraw.Draw.arc(xy, start, end, fill=None). - physicalattraction
I did something that may be useful here... stackoverflow.com/a/60392932/2836621 - Mark Setchell