50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
批量压缩 Assets/Model 下的大尺寸 PNG 贴图
|
|
将 4096x4096 缩小到 2048x2048,并优化文件大小
|
|
"""
|
|
import os
|
|
from PIL import Image
|
|
from pathlib import Path
|
|
|
|
def compress_texture(filepath, max_size=2048):
|
|
"""压缩单个贴图文件"""
|
|
try:
|
|
img = Image.open(filepath)
|
|
original_size = os.path.getsize(filepath)
|
|
|
|
# 如果图片大于目标尺寸,缩放
|
|
if img.width > max_size or img.height > max_size:
|
|
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
|
print(f" 缩放: {img.width}x{img.height}")
|
|
|
|
# 优化并保存
|
|
img.save(filepath, 'PNG', optimize=True, compress_level=9)
|
|
new_size = os.path.getsize(filepath)
|
|
|
|
saved_mb = (original_size - new_size) / (1024 * 1024)
|
|
print(f" 节省: {saved_mb:.2f} MB ({original_size/(1024*1024):.2f} -> {new_size/(1024*1024):.2f} MB)")
|
|
return saved_mb
|
|
except Exception as e:
|
|
print(f" 错误: {e}")
|
|
return 0
|
|
|
|
def main():
|
|
assets_path = Path("Assets/Model")
|
|
total_saved = 0
|
|
processed = 0
|
|
|
|
# 查找所有超过 10MB 的 PNG 文件
|
|
for png_file in assets_path.rglob("*.png"):
|
|
file_size = png_file.stat().st_size
|
|
if file_size > 10 * 1024 * 1024: # 10 MB
|
|
print(f"\n处理: {png_file.name}")
|
|
saved = compress_texture(str(png_file))
|
|
total_saved += saved
|
|
processed += 1
|
|
|
|
print(f"\n完成! 处理了 {processed} 个文件,共节省 {total_saved:.2f} MB")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|