🎨🎵 图像音频处理

核心能力

🖼️ 图像批量处理

Pillow ImageMagick OpenCV
  • 批量压缩图片(保持质量)
  • 生成缩略图、统一尺寸
  • 格式转换(PNG/JPG/WebP)
  • 批量添加水印、边框
  • 图片批量重命名、分类

🔊 音频格式处理

FFmpeg pydub librosa
  • 音频格式转换(WAV/MP3/OGG)
  • 批量压缩(游戏资源优化)
  • 音频剪辑、拼接
  • 音量标准化
  • 批量添加淡入淡出

🎯 OCR 文字识别

  • 截图文字提取
  • 批量处理扫描文档
  • 表格识别转 Excel
  • 多语言 OCR(中英日韩)

🎮 游戏资源优化

  • 纹理图集(Sprite Sheet)生成
  • 音频资源批量压缩
  • 资源文件批量重命名规范
  • 重复资源检测

常用代码示例

图片批量压缩

from PIL import Image
import os

def compress_image(input_path, output_path, quality=85, max_size=(1920, 1080)):
    """压缩图片并保持质量"""
    with Image.open(input_path) as img:
        # 调整尺寸
        img.thumbnail(max_size, Image.Resampling.LANCZOS)
        
        # 保存压缩
        if img.mode in ('RGBA', 'P'):
            img = img.convert('RGB')
        img.save(output_path, 'JPEG', quality=quality, optimize=True)

# 批量处理
for file in os.listdir('images/'):
    if file.endswith(('.png', '.jpg', '.jpeg')):
        compress_image(f'images/{file}', f'compressed/{file}')
        print(f'Compressed: {file}')

音频格式转换

from pydub import AudioSegment
import os

def convert_audio(input_path, output_path, format='mp3'):
    """转换音频格式"""
    audio = AudioSegment.from_file(input_path)
    audio.export(output_path, format=format)

# 批量转换 WAV to MP3
for file in os.listdir('audio/'):
    if file.endswith('.wav'):
        name = file[:-4]
        convert_audio(f'audio/{file}', f'audio_mp3/{name}.mp3', 'mp3')
        print(f'Converted: {file}')

# 批量压缩
audio = AudioSegment.from_file('input.wav')
audio.export('output.ogg', format='ogg', parameters=['-q', '4'])  # 高质量压缩

生成缩略图

from PIL import Image
import os

def create_thumbnail(input_path, output_path, size=(256, 256)):
    """生成统一尺寸缩略图"""
    with Image.open(input_path) as img:
        # 保持比例裁剪
        img.thumbnail(size, Image.Resampling.LANCZOS)
        
        # 创建正方形背景
        background = Image.new('RGB', size, (0, 0, 0))
        
        # 居中粘贴
        offset = ((size[0] - img.width) // 2, (size[1] - img.height) // 2)
        background.paste(img, offset)
        
        background.save(output_path)

# 批量生成
for file in os.listdir('screenshots/'):
    if file.endswith(('.png', '.jpg')):
        create_thumbnail(f'screenshots/{file}', f'thumbs/{file}')

项目应用案例

🎮 游戏截图批量处理

  • 自动压缩游戏截图用于商店展示
  • 生成统一尺寸的缩略图
  • 批量添加游戏 Logo 水印

🔊 游戏音频资源优化

  • 批量压缩音效文件(减少包体)
  • 统一音频格式(Web游戏用 OGG)
  • 音量标准化处理

📸 OCR 辅助开发

  • 从设计稿提取文字
  • 批量识别测试截图中的文字
  • 表格数据提取

📚 参考资料

在线文档

推荐书籍

  • 《计算机图形学》 - 图像处理基础
  • 《数字音频处理》 - 音频技术详解