Adding borders to an image using python

You can create a new image with the desired new size, and paste the old image in the center, then saving it. If you want, you can overwrite the original image (are you sure? ;o)

import Image

old_im ='someimage.jpg')
old_size = old_im.size

new_size = (800, 800)
new_im ="RGB", new_size)   ## luckily, this is already black!
box = tuple((n - o) // 2 for n, o in zip(new_size, old_size))
new_im.paste(old_im, box)

You can also set the color of the new border with a third argument of (for example:"RGB", new_size, "White"))

Leave a Comment