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?
PIL.ImageDraw.Draw.arc(xy, start, end, fill=None)
. – Ambagesalpha.paste(circle.crop((rad+1, rad+1, rad*2+1, rad*2+1)), (w-rad, h-rad))
. Another note: PIL version 8.2.0 added the methodrounded_rectangle
which should make this even easier. – Mcchesney