forked from kpiatan/ros_display_emotions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_images.py
More file actions
52 lines (40 loc) · 1.73 KB
/
resize_images.py
File metadata and controls
52 lines (40 loc) · 1.73 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import cv2
import numpy as np
import os
def resize_and_pad_lr(image):
target_width = 1024
target_height = 600
resized_width = 800
resized = cv2.resize(image, (resized_width, target_height), interpolation=cv2.INTER_AREA)
left_pad = (target_width - resized_width) // 2 # e.g. 112
right_pad = target_width - resized_width - left_pad # e.g. 112
output = np.zeros((target_height, target_width, 3), dtype=resized.dtype)
# Place resized image in the center
output[:, left_pad:left_pad+resized_width] = resized
# Fill left padding with leftmost column repeated
left_column = resized[:, 0:1, :]
for i in range(left_pad):
output[:, i:i+1, :] = left_column
# Fill right padding with rightmost column repeated
right_column = resized[:, -1:, :]
for i in range(right_pad):
output[:, left_pad + resized_width + i:left_pad + resized_width + i + 1] = right_column
return output
def process_folder(input_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp')):
input_path = os.path.join(input_folder, filename)
img = cv2.imread(input_path)
if img is None:
print(f"Failed to load {input_path}")
continue
out = resize_and_pad_lr(img)
output_path = os.path.join(output_folder, filename)
cv2.imwrite(output_path, out)
print(f"Processed and saved: {output_path}")
if __name__ == "__main__":
input_folder = "src/cropped/4_3"
output_folder = "src/cropped/1024_600"
process_folder(input_folder, output_folder)