Skip to content

Instantly share code, notes, and snippets.

@sohrabhamza
Last active November 2, 2022 14:26
Show Gist options
  • Save sohrabhamza/a094fef145d8b8f4771813db13f221d1 to your computer and use it in GitHub Desktop.
Save sohrabhamza/a094fef145d8b8f4771813db13f221d1 to your computer and use it in GitHub Desktop.
Add black bars to the top or bottom of an image to make them all square (for social media like Instagram)
from PIL import Image
import glob
import os
os.mkdir(("Output"))
i = 1
for myIm in glob.glob("*.jpg"):
im = Image.open(myIm)
negatorX = 1
negatorY = 1
if im.size[0] > im.size[1]: # Horizontal image
size = (im.size[0], im.size[0])
imageLoc = (size[0]//2)-(im.size[1]//2)
negatorX = 0
else: # vertical image
size = (im.size[1], im.size[1])
imageLoc = (size[0]//2)-(im.size[0]//2)
negatorY = 0
back = Image.new("RGB", size)
back.paste(im, (imageLoc * negatorX, imageLoc * negatorY))
back.save("Output/"+str(i)+".jpg", "JPEG")
i = i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment