我建议使用 PIL 的内置expand()
功能,它允许您向图像添加任何颜色和宽度的边框。
所以,从这个开始:
#!/usr/bin/env python3
from PIL import Image, ImageOps
# Open image
im = Image.open('start.png')
# Add border and save
bordered = ImageOps.expand(im, border=10, fill=(0,0,0))
bordered.save('result.png')
如果您想在顶部/底部从左到右使用不同大小的边框,请给出两个宽度:
bordered = ImageOps.expand(im, border=(10,50), fill=(0,0,0))
如果您希望所有边都有不同大小的边框,请指定 4 个宽度:
bordered = ImageOps.expand(im, border=(10,40,80,120), fill=(0,0,0))
Keywords:PIL、Pillow、ImageOps、Python、边框、边框、外边框、添加边框、展开、填充、范围、图像、图像处理。