In [1]:
from PIL import Image
In [5]:
#https://note.nkmk.me/en/python-pillow-add-margin-expand-canvas/
In [2]:
im = Image.open("36292_page_white_magnify_icon.png")
In [4]:
im
Out[4]:
In [6]:
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
In [16]:
im_new = add_margin(im, 
                    40, # top
                    0, # right
                    0, # bottom
                    0, #left
                    (56,247,66)) # color
                    #(255,255,255,0)) # color
In [17]:
im_new
Out[17]:
In [ ]: