-
Notifications
You must be signed in to change notification settings - Fork 1
/
resizeImages.py
executable file
·38 lines (28 loc) · 1.09 KB
/
resizeImages.py
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
from PIL import Image
import os
import numpy as np
# Input and output directories
input_dir = './eu_dataset/images/'
output_dir = './eu_dataset/images_resized/'
# Desired dimensions
new_width, new_height = 256, 160 #1280, 720
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# List all files in the input directory
files = os.listdir(input_dir)
for file in files:
if file.endswith('.jpg') or file.endswith('.png') or file.endswith('.jpeg'):
try:
# Open image
img = Image.open(os.path.join(input_dir, file))
# Resize using PIL
resized_img = img.resize((new_width, new_height))
resized_img = resized_img.convert('RGB')
# Convert to numpy array for saving
img_array = np.array(resized_img)
# Save the resized image
output_path = os.path.join(output_dir, file)
Image.fromarray(img_array).save(output_path)
print(f"Resized {file} and saved to {output_path}")
except Exception as e:
print(f"Failed to process {file}: {e}")