How to make circular image PIL

PHOTO EMBED

Sat Apr 20 2024 10:35:22 GMT+0000 (Coordinated Universal Time)

Saved by @amramroo ##python #coding #python #image #pil #pillow

from PIL import Image, ImageDraw, ImageOps

def convert_image_to_circle(image_path):
    my_image = Image.open(image_path)
    # create mask 
    mask = Image.new('L', my_image.size)
    draw = ImageDraw.Draw(mask)
    # draw white circle
    draw.ellipse((0, 0) + mask.size, fill = 255)

    # create output image
    output = ImageOps.fit(my_image, mask.size, centering=(0.5, 0.5))
    output.putalpha(mask)
    output.save('out.png')



convert_image_to_circle('test.png')
content_copyCOPY