File size: 949 Bytes
4a1207e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)