104 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			104 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
| #!/usr/bin/env python3
 | ||
| """
 | ||
| 快速图像转换脚本 - 专门处理胡汉三千年项目的PPM文件
 | ||
| """
 | ||
| 
 | ||
| import os
 | ||
| import sys
 | ||
| from pathlib import Path
 | ||
| 
 | ||
| def quick_install():
 | ||
|     """快速安装依赖"""
 | ||
|     import subprocess
 | ||
|     
 | ||
|     print("🔧 安装图像处理依赖...")
 | ||
|     packages = ['Pillow', 'svgwrite']
 | ||
|     
 | ||
|     for package in packages:
 | ||
|         try:
 | ||
|             subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
 | ||
|             print(f"✅ {package} 安装成功")
 | ||
|         except:
 | ||
|             print(f"❌ {package} 安装失败")
 | ||
| 
 | ||
| def convert_ppm_to_png_simple(ppm_path):
 | ||
|     """简单的PPM到PNG转换"""
 | ||
|     try:
 | ||
|         from PIL import Image
 | ||
|         
 | ||
|         # 打开PPM文件
 | ||
|         img = Image.open(ppm_path)
 | ||
|         
 | ||
|         # 转换为RGB(如果需要)
 | ||
|         if img.mode != 'RGB':
 | ||
|             img = img.convert('RGB')
 | ||
|         
 | ||
|         # 生成输出文件名
 | ||
|         output_path = str(Path(ppm_path).with_suffix('.png'))
 | ||
|         
 | ||
|         # 保存为PNG
 | ||
|         img.save(output_path, 'PNG', optimize=True)
 | ||
|         
 | ||
|         # 显示文件大小对比
 | ||
|         original_size = os.path.getsize(ppm_path) / (1024*1024)  # MB
 | ||
|         new_size = os.path.getsize(output_path) / (1024*1024)     # MB
 | ||
|         
 | ||
|         print(f"✅ {Path(ppm_path).name} -> {Path(output_path).name}")
 | ||
|         print(f"   原始: {original_size:.1f}MB -> 转换后: {new_size:.1f}MB")
 | ||
|         print(f"   压缩率: {(1-new_size/original_size)*100:.1f}%")
 | ||
|         
 | ||
|         return output_path
 | ||
|         
 | ||
|     except ImportError:
 | ||
|         print("❌ 需要安装 Pillow: pip install Pillow")
 | ||
|         return None
 | ||
|     except Exception as e:
 | ||
|         print(f"❌ 转换失败: {e}")
 | ||
|         return None
 | ||
| 
 | ||
| def batch_convert_images():
 | ||
|     """批量转换images目录下的所有PPM文件"""
 | ||
|     images_dir = Path("images")
 | ||
|     
 | ||
|     if not images_dir.exists():
 | ||
|         print("❌ images目录不存在")
 | ||
|         return
 | ||
|     
 | ||
|     # 查找所有PPM文件
 | ||
|     ppm_files = list(images_dir.rglob('*.ppm'))
 | ||
|     
 | ||
|     if not ppm_files:
 | ||
|         print("❌ 未找到PPM文件")
 | ||
|         return
 | ||
|     
 | ||
|     print(f"🔍 找到 {len(ppm_files)} 个PPM文件")
 | ||
|     
 | ||
|     total_original_size = 0
 | ||
|     total_new_size = 0
 | ||
|     converted_count = 0
 | ||
|     
 | ||
|     for ppm_file in ppm_files:
 | ||
|         print(f"\n📁 处理: {ppm_file.relative_to(images_dir)}")
 | ||
|         
 | ||
|         result = convert_ppm_to_png_simple(str(ppm_file))
 | ||
|         if result:
 | ||
|             converted_count += 1
 | ||
|             total_original_size += os.path.getsize(str(ppm_file))
 | ||
|             total_new_size += os.path.getsize(result)
 | ||
|     
 | ||
|     print(f"\n🎉 批量转换完成!")
 | ||
|     print(f"   转换文件数: {converted_count}/{len(ppm_files)}")
 | ||
|     print(f"   总大小: {total_original_size/(1024*1024):.1f}MB -> {total_new_size/(1024*1024):.1f}MB")
 | ||
|     print(f"   总体压缩率: {(1-total_new_size/total_original_size)*100:.1f}%")
 | ||
| 
 | ||
| def main():
 | ||
|     if len(sys.argv) > 1 and sys.argv[1] == '--install':
 | ||
|         quick_install()
 | ||
|     else:
 | ||
|         batch_convert_images()
 | ||
| 
 | ||
| if __name__ == '__main__':
 | ||
|     main()
 | ||
| 
 | ||
| 
 |