-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_size.py
36 lines (27 loc) · 1.36 KB
/
set_size.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
import os
from PIL import Image
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def resize_images_in_folder(folder_path, output_folder_path, size):
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
print(len(image_files))
total_files = len(image_files)
logging.info(f"Found {total_files} images to resize.")
for i, filename in enumerate(image_files):
try:
img_path = os.path.join(folder_path, filename)
img = Image.open(img_path)
img = img.resize(size, Image.ANTIALIAS)
output_path = os.path.join(output_folder_path, filename)
img.save(output_path)
if (i + 1) % 100 == 0 or i + 1 == total_files:
logging.info(f"Processed {i + 1}/{total_files} images")
except Exception as e:
logging.error(f"Failed to process {filename}: {e}")
# Example usage
input_folder = '/home/huy/data1tb/Restormer/SR_restormer/data/SR/test/rain'
output_folder = '/home/huy/data1tb/Restormer/SR_restormer/data/SR/test/rain'
new_size = (300, 300) # New size in pixels
resize_images_in_folder(input_folder, output_folder, new_size)