-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_images.py
32 lines (27 loc) · 1.18 KB
/
compress_images.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
import os
import tinify
from config import TINIFY_API_KEY, INPUT_IMAGES_DIR, COMPRESSED_IMAGES_DIR
# Set the API key for TinyPNG
tinify.key = TINIFY_API_KEY
def compress_images(input_path, output_path):
"""
Compresses all images in the input directory using TinyPNG API
and saves them to the output directory.
"""
if not os.path.exists(output_path):
os.makedirs(output_path)
for root, _, files in os.walk(input_path):
for file in files:
if file.lower().endswith((".jpg", ".jpeg", ".png")):
input_file = os.path.join(root, file)
relative_path = os.path.relpath(input_file, input_path)
output_file = os.path.join(output_path, relative_path)
os.makedirs(os.path.dirname(output_file), exist_ok=True)
try:
print(f"Compressing: {input_file} -> {output_file}")
source = tinify.from_file(input_file)
source.to_file(output_file)
except Exception as e:
print(f"Error compressing {input_file}: {e}")
if __name__ == "__main__":
compress_images(INPUT_IMAGES_DIR, COMPRESSED_IMAGES_DIR)