Skip to content

Instantly share code, notes, and snippets.

@bersling
Last active November 1, 2020 13:51
Show Gist options
  • Select an option

  • Save bersling/6b3127bcc870e9708397c08cbd7f9256 to your computer and use it in GitHub Desktop.

Select an option

Save bersling/6b3127bcc870e9708397c08cbd7f9256 to your computer and use it in GitHub Desktop.
Adding White Borders to an Image (for Instagram) through the commandline

Why?

Insta doesn't let you post images taller than 4:5. What you can do is to add white borders left and right to your image, then you can still upload the entire image. This script is doing this.

Requirements

  • python3
  • the "convert" command

Usage

python3 addborders.py yourfile.jpg

import subprocess
import sys
from PIL import Image
# Read the filename from the command line args
filename = sys.argv[1]
# Read in the image
image = Image.open(filename)
# Get with, height
width, height = image.size
print('Width: ' + str(width))
print('Height: ' + str(height))
# Calculate current aspect ratio of the image
aspect_ratio = width / height
# Define the desired aspect ratio. Insta allows max 4:5 so we'll use this here.
desired_aspect_ratio = 4 / 5
# The equation to determine how much border (padding) we need left + right
# (width + x) / height = desired_aspect_ratio
# x = desired_aspect_ratio * height - width
padding = desired_aspect_ratio * height - width
print('Padding: ' + str(padding))
# Calling the convert script, divide padding by two because it'll be added left and right.
subprocess.call(['convert', filename, '-bordercolor', 'white', '-border', str(padding/2) + 'x0', filename + '.edited.jpg'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment