次のページを参考にしました。
Python, Pillowで画像の上下左右に余白を追加しサイズ変更
add_mergin.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# add_mergin.py
#
# Oct/02/2018
#
# --------------------------------------------------------------------
import sys
from PIL import Image
# --------------------------------------------------------------------
def add_margin(pil_img, top, right, bottom, left, color):
width, height = pil_img.size
new_width = width + right + left
new_height = height + top + bottom
result = Image.new(pil_img.mode, (new_width, new_height), color)
result.paste(pil_img, (left, top))
return result
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_in = sys.argv[1]
file_out = sys.argv[2]
sys.stderr.write(file_in + "\n")
sys.stderr.write(file_out + "\n")
#
img = Image.open(file_in)
img = img.convert("RGBA")
width, height = img.size
sys.stderr.write("%d\t%d\n" % (width,height))
#
top = 60
right = 40
left = 40
bottom = 0
img = add_margin(img, top, right, bottom, left, (0, 0, 255))
#
width, height = img.size
sys.stderr.write("%d\t%d\n" % (width,height))
#
img.save(file_out)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
実行方法
./add_mergin.py in01.jpg out01.png