import os import cv2 # Function to resize image def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA): dim = None (h, w) = image.shape[:2] if width is None and height is None: return image if width is None: r = height / float(h) dim = (int(w * r), height) else: r = width / float(w) dim = (width, int(h * r)) resized = cv2.resize(image, dim, interpolation=inter) return resized # Function to convert and resize images def convert_and_resize_images_in_folder(folder_path): for filename in os.listdir(folder_path): if filename.lower().endswith(('.png', '.jpeg', '.bmp', '.tiff', '.gif', '.jpg')): file_path = os.path.join(folder_path, filename) # Read the image image = cv2.imread(file_path) if image is None: print(f"Error reading {file_path}") continue # Resize the image resized_image = image_resize(image, height=1080) # Construct the output file name base, ext = os.path.splitext(filename) output_file_path = os.path.join(folder_path, base + '.jpg') # Save the image in JPEG format cv2.imwrite(output_file_path, resized_image) print(f"Converted and resized {file_path} to {output_file_path}") if __name__ == "__main__": folder_path = '.' # current folder convert_and_resize_images_in_folder(folder_path)