Embedding an Image for Instagram

This is a short python program which uses PIL (Python imaging library or Pillow) to perform its magic. Cut and paste or email me for the source (Or even better sign up for my page and I'll use that email).

It takes an input image and places it in a 2048x2048 square with a white background.

#!/usr/bin/python3
#
#  (c) 2023 Treadco
#

import numpy as np
import sys
from PIL import Image

def main():
    try:
      image = Image.open(sys.argv[1])
    except IndexError:
      print("Could not open the input \nUsage to_instagram inputfile outputname")
      sys.exit()

    if len( sys.argv) < 3:
        outputfilename = "output.jpg"
    else: 
        outputfilename = sys.argv[2]

    iarray = np.array(image.convert("RGB"))
    nx = iarray.shape[0]
    ny = iarray.shape[1]
    inew = Image.new( "RGB", [2048,2048], (255,255,255))
   
    if nx > ny :
        simage = image.resize([ int((2048.*ny)/nx),2048 ])
        inew.paste( simage, [int((2048-int((2048.*ny)/nx))/2),0])
    else:
        simage = image.resize([2048, int((2048.*nx)/ny) ])
        inew.paste( simage, [0,int((2048-int((2048.*nx)/ny))/2)])

    inew.save(outputfilename)
    
 

main()