How does Python3 use PIL to add rounded corners and black borders to images in batches

I. Introduction

First look at the effect:
original image:
insert image description here
processed image: rounded corners + black border
insert image description here

2. Principle

PIL adds rounded corner logic to the image:

  • 1. Create a black transparent square with a side length equal to the corner diameter
    insert image description here

  • 2. Inside the black square, create a white opaque circle with the corner radius
    insert image description here

  • 3. Create a white opaque filter layer with the same size as the original image to cut the rounded corners of the image
    insert image description here

  • 4. Cut the picture in step 2 into four corners, paste them to the four corners of the filter layer in step 3, we get a filter layer
    insert image description here

  • 5. Use the Image.putalpha function to replace the aplha layer of the original image, so the cutting is successful.
    insert image description here

  • 6. Then we use the ImageDraw class to draw a rounded rectangle on the original image
    insert image description here

3. Core API

First attach the official documentation of Pillow's Image Module:
https://pillow.readthedocs.io/en/stable/reference/Image.html

img = Image.New(): create an Image
draw = ImageDraw.Draw(img): draw
circle.crop(box)from an Image : return a rectangular area from an image
alpha.paste(im, box, mask): paste another image into this image
img.putalpha(alpha): add or replace the alpha layer in this image

4. Source code

Code directly below:

from PIL import Image, ImageDraw
import os

def circle_corner(img, radii):
    circle = Image.new('L', (radii * 2, radii * 2), 0)  # 创建黑色方形
    draw = ImageDraw.Draw(circle)
    draw.ellipse((0, 0, radii * 2, radii * 2), fill=255)  # 黑色方形内切白色圆形

    img = img.convert("RGBA")
    w, h = img.size

    #创建一个alpha层,存放四个圆角,使用透明度切除圆角外的图片
    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)  # 白色区域透明可见,黑色区域不可见

    # 添加圆角边框
    draw = ImageDraw.Draw(img)
    draw.rounded_rectangle(img.getbbox(), outline="black", width=3, radius=radii)
    return img


if __name__ == '__main__':
    radii = 20  # 圆角大小
    filePath = 'C:/Users/Desktop/Src'
    outPath = 'C:/Users/Desktop/Des'
    for i, j, k in os.walk(filePath):
        for filename in k:
            src = filePath + "/" + filename
            des = outPath + "/" + filename
            print(src)
            img = Image.open(src)
            img = circle_corner(img, radii)
            img.save(des, 'png', quality=100)

Guess you like

Origin blog.csdn.net/qq563129582/article/details/122300218