-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimise_assets.py
57 lines (46 loc) · 2.49 KB
/
optimise_assets.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
52
53
54
55
56
57
import os
from PIL import Image
import cairosvg
def optimize_images(directory, output_directory=None):
if output_directory is None:
output_directory = directory
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for filename in os.listdir(directory):
try:
if filename.endswith(('.jpg', '.jpeg', '.png', '.gif', '.svg', '.webp', '.ico')):
file_path = os.path.join(directory, filename)
file_name, file_extension = os.path.splitext(filename)
optimized_filename = file_name + '_optimised.webp'
optimized_file_path = os.path.join(output_directory, optimized_filename)
# Skip if the optimized file already exists
if os.path.exists(optimized_file_path):
print(f'Skipping already optimised file: {optimized_filename}')
continue
# Handle SVG and ICO separately
if file_extension.lower() in ['.svg', '.ico']:
# Catch parsing errors for SVG files
try:
cairosvg.svg2png(url=file_path, write_to=file_path + '.png')
file_path += '.png'
file_extension = '.png'
except Exception as e:
print(f'Error converting {filename}: {e}')
continue
# Open the image file
with Image.open(file_path) as img:
# Convert non-WebP images to WebP, and optimize if it's already WebP
if file_extension.lower() != '.webp' or (
file_extension.lower() == '.webp' and 'optimised' not in file_name):
# Resize logic (if needed)
# Example: img = img.resize((new_width, new_height), Image.ANTIALIAS)
# Reduce quality and save
img.save(optimized_file_path, 'WEBP', quality=80)
# Remove temporary PNG if created from SVG or ICO
if file_extension.lower() in ['.svg', '.ico']:
os.remove(file_path)
print(f'Processed and saved: {optimized_filename}')
except Exception as e:
print(f'Failed to process {filename}: {e}')
# Call the function with the directory of images
optimize_images(r'O:\usercontent\PYCHARMPROJECTS\Cityv2\res\CDN', r'O:\usercontent\PYCHARMPROJECTS\Cityv2\res\CDN\OPTIMISED')