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...
CodePudding user response:
This answer assumes you are able to use 
It checks for portrait or landscape because in portrait, the crop area fills the width and is offset from the height; vice versa in landscape. You could probably do it in one statement with clever min and max statements if you really wanted.


