IOS corner smoothing in python


I am working on a project that involves video frame manipulation, and one of the requirements is to add smooth rounded corners to the frames. I did some research on how to add rounded corners to images using PIL or any other image processing library, but all of them produce rough corners that look unprofessional.
Here is the code I tried :-

def add_corners(im, rad):
    circle = Image.new('L', (rad * 2, rad * 2), 0)
    draw = ImageDraw.Draw(circle)
    draw.ellipse((0, 0, rad * 2 - 1, rad * 2 - 1), fill=255)
    alpha = Image.new('L', im.size, 255)
    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

im = Image.open('sand.png')
im = round_corner_jpg(im, 50)
im.save('sand_corner.png')

enter image description here

and here is the result it produces, as can be seen in the above image, the corners are rough and have this pixelated view on them, they are not smooth just like we can do in figma using IOS corner smoothing:-
enter image description here

I also saw this question that also had the same requirements, but they didn’t show the code for how to implement it :- Any way to make nice antialiased round corners for images in python?

I want to make sure that there is no loss in the frame quality when adding the rounded corners and the corners should be smooth and better yet look like the corner smoothing in figma

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img