将散落在项目根目录的测试文件统一整理到tests目录中,提高项目结构的清晰度和可维护性。 主要变更: 1. 移动测试文件:将根目录的test_*.py文件移动到tests/目录 2. 移动测试资源:将test_*.png图片文件移动到tests/目录 3. 移动测试输出:将test_output.dci文件移动到tests/目录 4. 清理环境:删除不需要的test_env虚拟环境目录 5. 更新文档:更新tests/README.md,详细记录所有测试文件分类 移动的文件包括: - test_advanced_compression.py - 高级压缩功能测试 - test_border_color.py - 边框颜色功能测试 - test_categories.py - 分类系统测试 - test_quality_feature.py - 图片质量功能测试 - test_transparent_preview.py - 透明预览测试 - test_pure_python_implementation.py - 纯Python实现测试 - test_pure_python_simple.py - 简单纯Python测试 - simple_quality_test.py - 简单质量测试脚本 - test_preview_*.png - 预览测试图片 - test_transparent_preview.png - 透明预览测试图片 这次整理确保了: - 项目根目录更加清洁,只保留核心配置文件 - 所有测试相关文件集中管理,便于维护和执行 - 测试文档完整记录了所有测试文件的用途和分类 - 符合标准的Python项目结构规范 refactor: Organize test file structure by moving all to tests directory Consolidated scattered test files from project root to tests directory to improve project structure clarity and maintainability. Key changes: 1. Move test files: Relocated test_*.py files from root to tests/ 2. Move test assets: Relocated test_*.png image files to tests/ 3. Move test output: Relocated test_output.dci file to tests/ 4. Clean environment: Removed unnecessary test_env virtual environment 5. Update documentation: Enhanced tests/README.md with detailed file classification Moved files include: - test_advanced_compression.py - Advanced compression feature tests - test_border_color.py - Border color functionality tests - test_categories.py - Category system tests - test_quality_feature.py - Image quality feature tests - test_transparent_preview.py - Transparent preview tests - test_pure_python_implementation.py - Pure Python implementation tests - test_pure_python_simple.py - Simple pure Python tests - simple_quality_test.py - Simple quality testing script - test_preview_*.png - Preview test images - test_transparent_preview.png - Transparent preview test image This reorganization ensures: - Cleaner project root with only core configuration files - Centralized test management for easier maintenance and execution - Complete test documentation with proper file categorization - Compliance with standard Python project structure conventions
121 lines
3.6 KiB
Python
121 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试WebP和PNG高级压缩设置功能
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from PIL import Image, ImageDraw
|
|
from io import BytesIO
|
|
|
|
def create_complex_test_image(size=(256, 256)):
|
|
"""创建复杂的测试图像,包含透明度和细节"""
|
|
img = Image.new('RGBA', size, (0, 0, 0, 0)) # 透明背景
|
|
draw = ImageDraw.Draw(img)
|
|
|
|
# 添加复杂的图案来更好地展示压缩差异
|
|
for i in range(0, size[0], 16):
|
|
for j in range(0, size[1], 16):
|
|
# 创建带透明度的彩色方块
|
|
alpha = (i + j) % 256
|
|
color = (i % 256, j % 256, (i+j) % 256, alpha)
|
|
draw.rectangle([i, j, i+15, j+15], fill=color)
|
|
|
|
return img
|
|
|
|
def test_webp_settings():
|
|
"""测试WebP的各种设置"""
|
|
print("测试WebP高级设置...")
|
|
|
|
img = create_complex_test_image()
|
|
|
|
# 测试不同的WebP设置
|
|
settings = [
|
|
{"name": "标准有损", "lossless": False, "quality": 80, "alpha_quality": 100},
|
|
{"name": "无损压缩", "lossless": True, "quality": 80, "alpha_quality": 100},
|
|
{"name": "低Alpha质量", "lossless": False, "quality": 80, "alpha_quality": 50},
|
|
{"name": "极低Alpha质量", "lossless": False, "quality": 80, "alpha_quality": 10},
|
|
]
|
|
|
|
results = []
|
|
|
|
for setting in settings:
|
|
webp_bytes = BytesIO()
|
|
|
|
if setting["lossless"]:
|
|
img.save(webp_bytes, format='WEBP', lossless=True)
|
|
else:
|
|
img.save(webp_bytes, format='WEBP', quality=setting["quality"], alpha_quality=setting["alpha_quality"])
|
|
|
|
size = len(webp_bytes.getvalue())
|
|
results.append((setting["name"], size))
|
|
|
|
print(f" {setting['name']:15}: {size:6d} 字节")
|
|
|
|
return results
|
|
|
|
def test_png_settings():
|
|
"""测试PNG的压缩等级设置"""
|
|
print("\n测试PNG压缩等级...")
|
|
|
|
img = create_complex_test_image()
|
|
|
|
results = []
|
|
|
|
for compress_level in range(0, 10): # PNG压缩等级 0-9
|
|
png_bytes = BytesIO()
|
|
img.save(png_bytes, format='PNG', compress_level=compress_level)
|
|
size = len(png_bytes.getvalue())
|
|
results.append((f"等级 {compress_level}", size))
|
|
|
|
print(f" 压缩等级 {compress_level}: {size:6d} 字节")
|
|
|
|
return results
|
|
|
|
def main():
|
|
"""运行所有测试"""
|
|
print("WebP和PNG高级压缩设置测试")
|
|
print("=" * 50)
|
|
|
|
try:
|
|
# 测试WebP设置
|
|
webp_results = test_webp_settings()
|
|
|
|
# 测试PNG设置
|
|
png_results = test_png_settings()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("测试总结:")
|
|
|
|
# WebP结果分析
|
|
webp_sizes = [size for _, size in webp_results]
|
|
print(f"WebP 文件大小范围: {min(webp_sizes)} - {max(webp_sizes)} 字节")
|
|
|
|
# PNG结果分析
|
|
png_sizes = [size for _, size in png_results]
|
|
print(f"PNG 文件大小范围: {min(png_sizes)} - {max(png_sizes)} 字节")
|
|
|
|
# 验证设置是否有效
|
|
webp_effective = max(webp_sizes) > min(webp_sizes)
|
|
png_effective = max(png_sizes) > min(png_sizes)
|
|
|
|
print(f"WebP 设置有效性: {'✓' if webp_effective else '✗'}")
|
|
print(f"PNG 设置有效性: {'✓' if png_effective else '✗'}")
|
|
|
|
if webp_effective and png_effective:
|
|
print("\n🎉 所有高级压缩设置功能正常工作!")
|
|
return True
|
|
else:
|
|
print("\n❌ 某些设置可能无效")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"\n测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|