可以利用Pillow的ImageOps模块实现图像的边界扩展(有点类似于Photoshop扩大画布的操作,各个方向的扩展的大小可以自行调节),示例代码:
from PIL import Image, ImageOps
img = Image.open('test.jpg')
img.show()
left_pad = 100
top_pad = 50
right_pad = 160
bottom_pad = 80
padding = (left_pad, top_pad, right_pad, bottom_pad)
img2 = ImageOps.expand(img, padding)
img2.show()