2024-05-07 17:00:24 -05:00
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
from PIL import Image
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
def compute_hash(file_path):
|
|
|
|
with open(file_path, 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
hash_value = hashlib.md5(content).hexdigest()
|
|
|
|
return hash_value
|
|
|
|
|
|
|
|
def compress_and_save(source_dir, target_dir, hash_dir):
|
|
|
|
if not os.path.exists(target_dir):
|
|
|
|
os.makedirs(target_dir)
|
|
|
|
|
|
|
|
if not os.path.exists(hash_dir):
|
|
|
|
os.makedirs(hash_dir)
|
|
|
|
|
2024-10-14 13:28:30 -05:00
|
|
|
processed_files = set()
|
|
|
|
|
2024-05-07 17:00:24 -05:00
|
|
|
for root, dirs, files in os.walk(source_dir):
|
|
|
|
for file_name in files:
|
2024-10-14 13:28:30 -05:00
|
|
|
source_path = os.path.join(root, file_name)
|
|
|
|
relative_path = os.path.relpath(source_path, source_dir)
|
|
|
|
target_path = os.path.join(target_dir, relative_path)
|
|
|
|
hash_file_path = os.path.join(hash_dir, relative_path + '.hash')
|
|
|
|
|
|
|
|
processed_files.add(relative_path)
|
|
|
|
|
|
|
|
original_hash = compute_hash(source_path)
|
|
|
|
|
|
|
|
if not os.path.exists(hash_file_path) or open(hash_file_path, 'r').read() != original_hash:
|
|
|
|
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
|
|
|
|
|
|
|
if file_name.lower().endswith('.png'):
|
2024-05-07 17:00:24 -05:00
|
|
|
with Image.open(source_path) as img:
|
|
|
|
img = img.convert('RGBA')
|
|
|
|
img.save(target_path, format='PNG', optimize=True)
|
2024-10-14 13:28:30 -05:00
|
|
|
else:
|
|
|
|
shutil.copyfile(source_path, target_path)
|
2024-05-07 17:00:24 -05:00
|
|
|
|
2024-10-14 13:28:30 -05:00
|
|
|
os.makedirs(os.path.dirname(hash_file_path), exist_ok=True)
|
|
|
|
with open(hash_file_path, 'w') as hash_file:
|
|
|
|
hash_file.write(original_hash)
|
2024-05-07 17:00:24 -05:00
|
|
|
|
2024-10-14 13:28:30 -05:00
|
|
|
for root, dirs, files in os.walk(target_dir, topdown=False):
|
|
|
|
for file_name in files:
|
|
|
|
file_path = os.path.join(root, file_name)
|
|
|
|
relative_path = os.path.relpath(file_path, target_dir)
|
|
|
|
if relative_path not in processed_files:
|
|
|
|
os.remove(file_path)
|
|
|
|
hash_file_path = os.path.join(hash_dir, relative_path + '.hash')
|
|
|
|
if os.path.exists(hash_file_path):
|
|
|
|
os.remove(hash_file_path)
|
2024-05-07 17:00:24 -05:00
|
|
|
|
2024-10-14 13:28:30 -05:00
|
|
|
for dir_name in dirs:
|
|
|
|
dir_path = os.path.join(root, dir_name)
|
|
|
|
if not os.listdir(dir_path):
|
|
|
|
os.rmdir(dir_path)
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(hash_dir, topdown=False):
|
|
|
|
for dir_name in dirs:
|
|
|
|
dir_path = os.path.join(root, dir_name)
|
|
|
|
if not os.listdir(dir_path):
|
|
|
|
os.rmdir(dir_path)
|
|
|
|
|
|
|
|
source_directory_path = 'resources'
|
|
|
|
target_directory_path = 'optimizedResources'
|
2024-05-07 17:00:24 -05:00
|
|
|
hash_directory_path = 'hashes'
|
|
|
|
|
|
|
|
print("Optimizing images...")
|
|
|
|
compress_and_save(source_directory_path, target_directory_path, hash_directory_path)
|
2024-10-14 13:28:30 -05:00
|
|
|
print("Optimization complete.")
|