批量将图片转为圆角
将本地保存的图片全部转为圆角,代码如下:
from PIL import Image, ImageDraw
def circle_corner(img, radii):
# 画圆(用于分离4个角)
circle = Image.new('L', (radii * 2, radii * 2), 0) # 创建黑色方形
# circle.save('1.jpg','JPEG',qulity=100)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, radii * 2, radii * 2), fill=255) # 黑色方形内切白色圆形
# circle.save('2.jpg','JPEG',qulity=100)
img = img.convert("RGBA")
w, h = img.size
alpha = Image.new('L', img.size, 255)
alpha.paste(circle.crop((0, 0, radii, radii)), (0, 0)) # 左上角
alpha.paste(circle.crop((radii, 0, radii * 2, radii)),
(w - radii, 0)) # 右上角
alpha.paste(circle.crop((radii, radii, radii * 2, radii * 2)),
(w - radii, h - radii)) # 右下角
alpha.paste(circle.crop((0, radii, radii, radii * 2)),
(0, h - radii)) # 左下角
img.putalpha(alpha) # 白色区域透明可见,黑色区域不可见
return img
if __name__ == '__main__':
radii = 50
for i in range(1, 211):
img = Image.open(f'1 ({i}).jpg')
img = circle_corner(img, radii)
img.save(f'result({i}).png', 'png', quality=100)
纸上得来终觉浅,绝知此事要躬行!