# replace this with your own folder name # your folder should contain all images you want to squarify folder = 'myimages'
# name of folder the NEW images will be in new_folder = f'{folder}_new'
# if this folder doesn't exist, create it ifnot os.path.exists(new_folder): os.mkdir(new_folder)
# looping through all images in our folder for
filename in os.listdir(folder):
# ignoring anything that isn't an image # rememeber to change this is you're using jpg or something else if'.png'notin filename.lower(): continue
filepath = f'{folder}/{filename}'
# reading the image using cv2.imread # img is a numpy 3d array img = cv2.imread(filepath)
# extracting height/width of image h, w = img.shape[:2]
# px --> number of padding pixels to add on left/right # py --> number of padding pixels to add on top/bottom px, py = 0, 0
# checking if image height is more than image width if h > w: # calculating pixels to add to left/right # py will be 0 px = (h-w)//2 elif h # calculating pixels to add to top/bottom # py will be 0 py = (w-h)//2
# creating new image with padding newimg = cv2.copyMakeBorder( img, py, py, px, px, borderType=cv2.BORDER_CONSTANT, value=[255,255,255] # white (change this if you want other colors) )