-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.py
51 lines (40 loc) · 1.83 KB
/
optimize.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
39
40
41
42
43
44
45
46
47
48
49
50
51
from PIL import Image
import os
valid_extensions = [".jpg", ".jpeg", ".png"]
class color:
error: str = "\033[91m"
success: str = "\033[92m"
info: str = "\033[93m"
def resize_images(input_dir, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Traverse input directory recursively
for root, dirs, files in os.walk(input_dir):
# Determine relative path inside input directory
relative_root = os.path.relpath(root, input_dir)
# Construct corresponding output directory path
output_subdir = os.path.join(output_dir, relative_root)
# Create the corresponding output subdirectory if it doesn't exist
if not os.path.exists(output_subdir):
os.makedirs(output_subdir)
# Process each file in the current directory
for filename in files:
extension = os.path.splitext(filename)[1].lower()
if extension in valid_extensions:
try:
input_path = os.path.join(root, filename)
output_path = os.path.join(output_subdir, filename.split('.')[0] + '.jpg')
if os.path.exists(output_path):
continue
with Image.open(input_path) as img:
if img.mode != "RGB":
img = img.convert("RGB")
img.thumbnail((500, 400))
img.save(output_path, "JPEG", quality=80)
print(f"{color.success}{filename} resized and saved successfully.")
except Exception as e:
print(f"{color.error}Error processing {filename}: {e}")
# Example usage:
input_directory = "src/assets/imgs/works_src"
output_directory = "src/assets/imgs/works"
resize_images(input_directory, output_directory)