imageproxy icon indicating copy to clipboard operation
imageproxy copied to clipboard

Transformation to add white space around image

Open timuric opened this issue 5 years ago • 1 comments

I am looking for a way to add white space around image crop as illustrated in the image bellow. Anyone has ideas how to achieve that with the existing transformations?

image

timuric avatar Feb 28 '20 11:02 timuric

Hello, I found a very good answer to this question here: (https://stackoverflow.com/questions/11142851/adding-borders-to-an-image-using-python)

Below is one way to do it

import Image

old_im = Image.open('someimage.jpg')
old_size = old_im.size

new_size = (800, 800)
new_im = Image.new("RGB", new_size)   ## luckily, this is already black!
new_im.paste(old_im, ((new_size[0]-old_size[0])//2,
                      (new_size[1]-old_size[1])//2))

new_im.show()
# new_im.save('someimage.jpg')

by heltonbiker

Below is another using PIL

from PIL import Image, ImageOps
ImageOps.expand(Image.open('original-image.png'),border=300,fill='black').save('imaged-with-border.png')

by Igor Chubin

Charlz91108112 avatar Jun 05 '21 16:06 Charlz91108112