I need to resize the the images within a folder in Python. By using the code below, how can I have the same dimension of them in the end?
from PIL import Image
import os
path = "E:/Jupyter-Project/images/test/car/"
resize_ratio = 0.5
def resize_aspect_fit():
dirs = os.listdir(path)
for item in dirs:
if item == '.jpg':
continue
if os.path.isfile(path+item):
image = Image.open(path+item)
file_path, extension = os.path.splitext(path+item)
new_image_height = int(image.size[0] / (1/resize_ratio))
new_image_length = int(image.size[1] / (1/resize_ratio))
image = image.resize((new_image_height, new_image_length), Image.ANTIALIAS)
image.save(file_path + "_small" + extension, 'JPEG', quality=90)
resize_aspect_fit()
The code works perfect, but some images are 3 K, others are 4... and I do not really know how to modify it in order to achieve the same dimension.