My question is simple yet I haven't found any solution on Google, all the answers are for adding padding which in my case I don't want...
It's basically resizing images the WordPress way (resize and crop intelligently)... for square aspect ratio without pad... please help and thank you in advance.
Here is the image input example:
And here is the result I want for example (150x150 or any square size):
This is what I have so far tried:
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
from pathlib import Path
import argparse
import numpy
mypath = 'images'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
path = join(mypath, onlyfiles[n])
images[n] = cv2.imread(join(mypath, onlyfiles[n]),
cv2.IMREAD_UNCHANGED)
try:
img = cv2.imread(path, 1)
resized_dimensions = (400, 400)
resized_image = cv2.resize(img, resized_dimensions, interpolation=cv2.INTER_AREA)
if not cv2.imwrite('output/' +str(n)+ '.jpg', resized_image):
raise Exception("Could not write image")
except Exception as e:
print(str(e))
print("Images resized Successfully")
The code works but the images are distorted...



(h,w) = im.shape[:2]; a = min(h,w); im[(h-a)//2 : (h+a)//2, (w-a)//2 : (w+a)//2]. resize to taste. -- for resizing at the same time, I'd probably just calculate an affine matrix and use warpAffine -- I won't comment on all that code in the question. looks too complicated. half of it isn't even for resizing.