import cv2 import os y_folder = "./Input_Images" def crop_and_save_images(folder_path): # Get a list of all files in the folder files = os.listdir(folder_path) for file in files: # Construct the full file path file_path = os.path.join(folder_path, file) # Load the image img = cv2.imread(file_path) # Get the dimensions of the image h, w = img.shape[:2] # Determine the size of the crop crop_size = min(h, w) # Calculate the start coordinates of the crop start_y = (h - crop_size) // 2 start_x = (w - crop_size) // 2 # Perform the crop img_cropped = img[start_y : start_y + crop_size, start_x : start_x + crop_size] # Save the cropped image, overwriting the original image cv2.imwrite(file_path, img_cropped) # Example usage: crop_and_save_images(y_folder)