Files
qqbot/军团图切图.py

47 lines
1.7 KiB
Python

from PIL import Image, ImageDraw
def add_rounded_corners(image, radius, color):
# 创建一个与原始图像大小相同的掩膜图像
mask = Image.new("L", (1600,800), 0)
draw = ImageDraw.Draw(mask)
image = original_image
# 获取原始图像的宽度和高度
width, height = image.size
# 计算裁剪框的左上角和右下角坐标
left = (width - 1600) / 2
top = (height - 800) / 2
right = (width + 1600) / 2
bottom = (height + 800) / 2
# 使用crop()方法裁剪图像
cropped_image = image.crop((left, top, right, bottom))
# 在掩膜图像上绘制一个带有圆角的矩形
draw.pieslice([0, 0, radius * 2, radius * 2], 180, 270, fill=255)
draw.pieslice([cropped_image.width - 2 * radius, 0, cropped_image.width, 2 * radius], 270, 360, fill=255)
draw.pieslice([0, cropped_image.height - 2 * radius, 2 * radius, cropped_image.height], 90, 180, fill=255)
draw.pieslice([cropped_image.width - 2 * radius, cropped_image.height - 2 * radius, cropped_image.width, cropped_image.height], 0, 90, fill=255)
draw.rectangle([radius, 0, cropped_image.width - radius, cropped_image.height], fill=255)
draw.rectangle([0, radius, cropped_image.width, cropped_image.height - radius], fill=255)
# 创建一个新图像,并将原始图像放置在上面
result = Image.new("RGBA", cropped_image.size, (0, 0, 0, 0))
result.paste(cropped_image, (0, 0), mask)
return result
# 打开原始图片
original_image = Image.open("img.png").convert("RGBA")
# 添加圆角效果
rounded_image = add_rounded_corners(original_image, 160, (0, 0, 0, 0))
# 保存处理后的图片
#rounded_image.save("rounded_image.png")
rounded_image.show()