Skip to content

Instantly share code, notes, and snippets.

@mitchellvitez
Created May 22, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchellvitez/a4545e067eb5db99cd19dce6b260f444 to your computer and use it in GitHub Desktop.
Save mitchellvitez/a4545e067eb5db99cd19dce6b260f444 to your computer and use it in GitHub Desktop.
Adds a solid-colored border to images of a known width/height
# you will need a folder called `images` with pngs you want to add a border to,
# and a folder called `altered` where they'll be written to
from PIL import Image
import glob
WIDTH = 200
HEIGHT = 500
MARGIN = 10
COLOR = (255, 255, 255, 255) # RGBA white
bg_w, bg_h = (WIDTH + MARGIN * 2, HEIGHT + MARGIN * 2)
for fullname in glob.glob('images/*.png'):
name = fullname.split('/')[1].split('.')[0]
img = Image.open(fullname).convert('RGBA')
background = Image.new('RGBA', (bg_w, bg_h), COLOR)
offset = ((bg_w - WIDTH) // 2, (bg_h - HEIGHT) // 2)
background.paste(img, offset)
background.save(f'altered/{name}.png', 'png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment