Files
huhan3000/tools/data-processing/image-processing/quick_convert.py
ben 5b0a6c7bc1 重构程序文件目录结构并更新相关路径引用
- 创建新的目录结构:research/、tools/(含子目录)和apps/
- 移动核心理论文件到research/core-theory/
- 移动天山理论文件到research/specialized/
- 重组tools/目录为多个子目录:content-generation/、data-processing/等
- 更新所有文档中的路径引用,包括README.md、项目结构说明.md等
- 更新工作流文件和脚本中的路径引用
- 更新文档索引文件中的路径引用
2025-10-27 12:54:26 +00:00

104 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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()