Created
May 22, 2021 18:44
-
-
Save mitchellvitez/a4545e067eb5db99cd19dce6b260f444 to your computer and use it in GitHub Desktop.
Adds a solid-colored border to images of a known width/height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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