import cv2
import os


# A function to resize the images with longer side to num_pixels pixels with cubic interpolation and save to a new directory
def resize_images(path, new_path, num_pixels=300):
    if not os.path.exists(new_path):
        os.makedirs(new_path)
    for filename in os.listdir(path):
        if not filename.startswith(".") and (
            filename.endswith(".jpg")
            or filename.endswith(".jpeg")
            or filename.endswith(".png")
        ):
            img = cv2.imread(os.path.join(path, filename))
            height, width, channels = img.shape
            if height > width:
                new_height = num_pixels
                new_width = int(width * new_height / height)
            else:
                new_width = num_pixels
                new_height = int(height * new_width / width)
            img = cv2.resize(
                img, (new_width, new_height), interpolation=cv2.INTER_CUBIC
            )
            cv2.imwrite(os.path.join(new_path, filename), img)


# resize_images('./images/val2014', './resized_images/val2014')
# resize_images("./misc/article", "./misc/article/resized", 500)
resize_images("./cc12m_data/images_vqa", "./cc12m_data/resized_images_vqa", 500)