Quad Panels for Instagram Posting

I’ve set up another simple python program to produce an image made of four others. This is a step to fancy animations. Apparently panels of four things are more eye-catching than panels of one thing. It’s even more pronounced if only one panel is animated. That’ll take OpenCV to do automatically.

For what it’s worth, if you aren’t familiar with programming note how I’ve used a function to simplify the code. Once I read the documentation for OpenCV I’ll add a function to generate each frame. This function will look like main() but without any hard-wired constants.

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

import numpy as np
import sys
from PIL import Image

# return an image scaled to fit into xw,yw with background color
# defaults are for a 2048,2048 instagram image of 4 quadrants
def fitted_image(  an_image,xw=1024,yw=1024, background=(255,255,255)):
    iarray = np.array( an_image.convert("RGB") )
    nx = iarray.shape[0]
    ny = iarray.shape[1]
    inew = Image.new( "RGB", [xw,yw], (255,255,255))
   
    if nx > ny :
        simage = an_image.resize([ int((xw*ny)/nx),yw ])
        inew.paste( simage, [int((xw-int((xw*ny)/nx))/2),0])
    else:
        simage = an_image.resize([xw, int((yw*nx)/ny) ])
        inew.paste( simage, [0,int((yw-int((yw*nx)/ny))/2)])
    return inew

def main():
    if len( sys.argv) < 5:
      print("Could not open the input \nUsage to_quad ul ur ll lr outputname")
      sys.exit()

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

    inew = Image.new( "RGB", [2048,2048], (255,255,255))
    inew.paste( fitted_image(Image.open(sys.argv[1])), [0,0])
    inew.paste( fitted_image(Image.open(sys.argv[2])), [1024,0])
    inew.paste( fitted_image(Image.open(sys.argv[3])), [0,1024])
    inew.paste( fitted_image(Image.open(sys.argv[4])), [1024,1024])
       

    inew.save(outputfilename)
    

main()

Author: rharrisonauthor

International man of mystery. Well not really, although I can mangle several languages and even read the occasional hieroglyphic. A computer scientist, an author and one of the very few people who has both an NIH grant and had a book contract. An ex- booktrope author and a photographer.