python实现长方形图片变为椭圆

说多了都是泪,咱们废话不多说直接上代码,伙计们快来学习呀

from PIL import Image, ImageDraw


#   使图片变为椭圆,并且控制椭圆图片的弧度
def crop_to_ellipse(image_path, output_path):
    # 打开图片
    image = Image.open(image_path).convert("RGBA")
    width, height = image.size

    # 创建一个透明层作为遮罩层
    mask = Image.new("L", (width, height), 0)
    draw = ImageDraw.Draw(mask)

    # 绘制一个椭圆形的遮罩
    draw.rounded_rectangle([(0, 0), (width, height)], 20, fill=255)

    # 将遮罩应用到原图片上
    ellipse_image = Image.new("RGBA", (width, height))
    ellipse_image.paste(image, mask=mask)

    # 保存结果图片
    ellipse_image.save(output_path)


# 示例用法
image_path = ""
output_path = ""

crop_to_ellipse(image_path, output_path)

你可能感兴趣的:(python,前端,服务器)