-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresizeImages.py
More file actions
51 lines (34 loc) · 1.57 KB
/
Copy pathresizeImages.py
File metadata and controls
51 lines (34 loc) · 1.57 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
import os
from PIL import Image
"""
Resizes images in the input_folder to the specified height and width,
and saves them in the output_folder.
Args:
input_folder (str): Path to the folder containing the images to resize.
output_folder (str): Path to the folder to save resized images.
height (int): Desired height of the resized images.
width (int): Desired width of the resized images.
"""
def resize_images(input_folder, output_folder, height=256, width=384):
os.makedir(output_folder, exist_ok=True)
# Loop through all files in the input folder
for file_name in os.listdir(input_folder):
file_path = os.path.join(input_folder, file_name)
# Check if the file is an image
if os.path.isfile(file_path) and file_name.lower().endswith(('.png', '.jpg', '.jpeg')):
try:
# Open the image
with Image.open(file_path) as img:
# Resize the image
resized_img = img.resize((width, height))
# Save the resized image in the output folder
output_path = os.path.join(output_folder, file_name)
resized_img.save(output_path)
print(f"Resized and saved: {file_name}")
except Exception as e:
print(f"Error processing {file_name}: {e}")
if __name__ == "__main__":
# Adjust with the name of your data folder
sourceData = "./Data"
resizedData = "./ResizedData"
resize_images(sourceData, resizedData)