Pillow Image Processing Skill
English | 简体中文
This skill provides comprehensive image processing capabilities through executable scripts and reference documentation for Pillow (PIL).
When to Use This Skill
Activate this skill when the user requests:
- - Image editing operations (resize, crop, rotate, color adjustments)
- Batch processing multiple images
- Adding watermarks (text or image)
- Image format conversions
- Extracting image metadata or EXIF data
- Applying filters or effects
- Creating thumbnails
Core Capabilities
1. Image Editor (scripts/image_editor.py)
Edit single images with various operations:
Usage:
CODEBLOCK0
Options:
- -
--width WIDTH / --height HEIGHT: Resize dimensions - INLINECODE3 : Ignore aspect ratio when resizing
- INLINECODE4 : Crop rectangle
- INLINECODE5 : Rotate image
- INLINECODE6 : Flip image
- INLINECODE7 : Adjust brightness (0.0-2.0)
- INLINECODE8 : Adjust contrast (0.0-2.0)
- INLINECODE9 : Adjust color saturation (0.0-2.0)
- INLINECODE10 : Adjust sharpness (0.0-2.0)
- INLINECODE11 : Apply filter
- INLINECODE12 : Output format (JPEG, PNG, etc.)
- INLINECODE13 : JPEG quality (1-100)
Examples:
CODEBLOCK1
2. Batch Processor (scripts/batch_processor.py)
Process multiple images in parallel:
Usage:
CODEBLOCK2
Options:
- -
--pattern PATTERN: File pattern (e.g., *.jpg) - INLINECODE16 : Resize all images
- INLINECODE17 : Create thumbnails (maintains aspect)
- INLINECODE18 : Convert to grayscale
- INLINECODE19 : Adjust brightness
- INLINECODE20 : Convert format
- INLINECODE21 : JPEG quality
- INLINECODE22 : Number of parallel workers (default: 4)
Examples:
CODEBLOCK3
3. Watermark Tool (scripts/watermark.py)
Add text or image watermarks:
Usage:
CODEBLOCK4
Common Options:
- -
--position {top-left,top-right,bottom-left,bottom-right,center}: Position - INLINECODE25 : Transparency level
- INLINECODE26 : Margin from edge
Text Options:
- -
--font-size SIZE: Font size - INLINECODE28 : Text color (white/black/red/etc)
Image Options:
- -
--scale RATIO: Watermark scale (0.0-1.0)
Examples:
CODEBLOCK5
4. Image Info (scripts/image_info.py)
Extract image metadata and properties:
Usage:
CODEBLOCK6
Options:
- -
--format {text,json}: Output format - INLINECODE32 : Save to file
Provides:
- - File information (size, path)
- Image properties (dimensions, format, mode)
- Color information (bands, palette)
- EXIF data (if available)
- Metadata
Example:
CODEBLOCK7
Reference Documentation
references/common_operations.md
Comprehensive Pillow reference covering:
- - Opening and saving images
- Resizing and cropping
- Rotation and flipping
- Color adjustments and enhancements
- Filters and effects
- Drawing on images
- Image composition
- Working with channels
- EXIF data handling
- Performance tips
When to use: When Claude needs specific Pillow syntax or operation patterns.
references/best_practices.md
Best practices guide covering:
- - Format selection (JPEG vs PNG vs WebP)
- Resizing strategies
- Color mode conversion
- Memory management
- Watermarking strategies
- Filter application
- Optimization techniques
- Error handling patterns
- Common workflows
When to use: When designing image processing workflows or optimizing performance.
Workflow Guidelines
Step 1: Analyze Requirements
- - What format is the input image?
- What operations are needed?
- Is it a single image or batch?
- Are there quality requirements?
Step 2: Choose the Right Tool
- - Single image edit → INLINECODE35
- Multiple images → INLINECODE36
- Add watermark → INLINECODE37
- Need info → INLINECODE38
Step 3: Plan Operations
- - Apply operations in logical order
- Consider quality vs file size tradeoffs
- Validate input requirements
Step 4: Execute and Validate
- - Run the script with appropriate options
- Check output quality
- Verify file sizes and formats
Common Patterns
Pattern 1: Web Image Optimization
CODEBLOCK8
Pattern 2: Create Image Gallery
CODEBLOCK9
Pattern 3: Brand Images with Watermark
CODEBLOCK10
Pattern 4: Batch Format Conversion
CODEBLOCK11
Dependencies
CODEBLOCK12
Tips for Effective Use
- 1. Preserve originals: Never overwrite source images
- Use appropriate formats: JPEG for photos, PNG for graphics
- Optimize quality: Balance quality and file size
- Batch operations: Use batch processor for multiple images
- Check references: Consult reference docs for advanced operations
- Validate inputs: Check image format and size before processing
- Test first: Try operations on one image before batch processing
Limitations
- - Limited to 2D image processing (no video)
- Some EXIF data may not be preserved in all formats
- Font availability may vary by system
- Very large images may require significant memory
- Advanced photo editing (layers, masks) requires specialized tools
Troubleshooting
Import errors: Ensure Pillow is installed (pip install Pillow)
Font not found: Watermark script falls back to default font
Memory errors: Process large images in smaller batches
Format errors: Check input image format compatibility
RGBA to JPEG: Script automatically handles RGBA→RGB conversion
For detailed operations and troubleshooting, always refer to references/common_operations.md and references/best_practices.md.
Pillow 图像处理技能
本技能通过可执行脚本和 Pillow (PIL) 参考文档,提供全面的图像处理能力。
何时使用本技能
当用户请求以下内容时激活本技能:
- - 图像编辑操作(调整大小、裁剪、旋转、色彩调整)
- 批量处理多张图像
- 添加水印(文字或图像)
- 图像格式转换
- 提取图像元数据或 EXIF 数据
- 应用滤镜或特效
- 创建缩略图
核心能力
1. 图像编辑器 (scripts/image_editor.py)
通过多种操作编辑单张图像:
用法:
bash
python scripts/image_editor.py input.jpg output.jpg [选项]
选项:
- - --width WIDTH / --height HEIGHT:调整尺寸
- --no-aspect:调整大小时忽略宽高比
- --crop X Y WIDTH HEIGHT:裁剪矩形区域
- --rotate DEGREES:旋转图像
- --flip {horizontal,vertical}:翻转图像
- --brightness FACTOR:调整亮度(0.0-2.0)
- --contrast FACTOR:调整对比度(0.0-2.0)
- --color FACTOR:调整色彩饱和度(0.0-2.0)
- --sharpness FACTOR:调整锐度(0.0-2.0)
- --filter {blur,contour,detail,edge_enhance,emboss,sharpen,smooth}:应用滤镜
- --format FORMAT:输出格式(JPEG、PNG 等)
- --quality QUALITY:JPEG 质量(1-100)
示例:
bash
保持宽高比调整大小
python scripts/image_editor.py photo.jpg resized.jpg --width 800
多项操作
python scripts/image_editor.py input.jpg output.jpg \
--crop 100 100 800 600 \
--rotate 90 \
--brightness 1.2 \
--sharpen 1.5
2. 批量处理器 (scripts/batch_processor.py)
并行处理多张图像:
用法:
bash
python scripts/batchprocessor.py inputdir output_dir [选项]
选项:
- - --pattern PATTERN:文件模式(例如 *.jpg)
- --resize WIDTH HEIGHT:调整所有图像尺寸
- --thumbnail MAXW MAXH:创建缩略图(保持宽高比)
- --grayscale:转换为灰度图
- --brightness FACTOR:调整亮度
- --format FORMAT:转换格式
- --quality QUALITY:JPEG 质量
- --workers N:并行工作线程数(默认:4)
示例:
bash
创建缩略图
python scripts/batch_processor.py ./photos ./thumbs --thumbnail 300 300
批量转换并调整大小
python scripts/batch_processor.py ./raw ./processed \
--resize 1920 1080 \
--format JPEG \
--quality 90
3. 水印工具 (scripts/watermark.py)
添加文字或图像水印:
用法:
bash
python scripts/watermark.py input.jpg output.jpg --text 文字 [选项]
python scripts/watermark.py input.jpg output.jpg --image logo.png [选项]
通用选项:
- - --position {top-left,top-right,bottom-left,bottom-right,center}:位置
- --opacity 0-255:透明度级别
- --margin PIXELS:距边缘边距
文字选项:
- - --font-size SIZE:字号
- --color COLOR:文字颜色(white/black/red 等)
图像选项:
- - --scale RATIO:水印缩放比例(0.0-1.0)
示例:
bash
文字水印
python scripts/watermark.py photo.jpg marked.jpg \
--text © 2026 公司 \
--position bottom-right \
--opacity 128
Logo 水印
python scripts/watermark.py image.jpg output.jpg \
--image logo.png \
--scale 0.2 \
--position top-left
4. 图像信息 (scripts/image_info.py)
提取图像元数据和属性:
用法:
bash
python scripts/image_info.py image.jpg [选项]
选项:
- - --format {text,json}:输出格式
- --output FILE:保存到文件
提供信息:
- - 文件信息(大小、路径)
- 图像属性(尺寸、格式、模式)
- 色彩信息(通道、调色板)
- EXIF 数据(如有)
- 元数据
示例:
bash
显示信息
python scripts/image_info.py photo.jpg
保存为 JSON
python scripts/image_info.py photo.jpg -o info.json --format json
参考文档
references/common_operations.md
全面的 Pillow 参考文档,涵盖:
- - 打开和保存图像
- 调整大小和裁剪
- 旋转和翻转
- 色彩调整和增强
- 滤镜和特效
- 在图像上绘制
- 图像合成
- 通道处理
- EXIF 数据处理
- 性能优化建议
使用时机: 当 Claude 需要具体的 Pillow 语法或操作模式时。
references/best_practices.md
最佳实践指南,涵盖:
- - 格式选择(JPEG vs PNG vs WebP)
- 调整大小策略
- 色彩模式转换
- 内存管理
- 水印策略
- 滤镜应用
- 优化技术
- 错误处理模式
- 常见工作流程
使用时机: 当设计图像处理工作流程或优化性能时。
工作流程指南
步骤 1:分析需求
- - 输入图像是什么格式?
- 需要哪些操作?
- 是单张图像还是批量处理?
- 是否有质量要求?
步骤 2:选择合适的工具
- - 单张图像编辑 → imageeditor.py
- 多张图像 → batchprocessor.py
- 添加水印 → watermark.py
- 需要信息 → image_info.py
步骤 3:规划操作
- - 按逻辑顺序应用操作
- 考虑质量与文件大小的权衡
- 验证输入要求
步骤 4:执行并验证
- - 使用适当的选项运行脚本
- 检查输出质量
- 验证文件大小和格式
常见模式
模式 1:网页图像优化
bash
调整大小并优化网页显示
python scripts/image_editor.py large.jpg web.jpg \
--width 1200 \
--quality 85 \
--format JPEG
模式 2:创建图像画廊
bash
生成缩略图
python scripts/batch_processor.py ./originals ./gallery \
--thumbnail 400 400 \
--format JPEG \
--quality 90
模式 3:品牌图像加水印
bash
添加公司 Logo
python scripts/watermark.py product.jpg branded.jpg \
--image company_logo.png \
--position bottom-right \
--scale 0.15 \
--opacity 180
模式 4:批量格式转换
bash
将 PNG 转换为 JPEG
python scripts/batch_processor.py ./pngs ./jpegs \
--format JPEG \
--quality 95
依赖项
bash
pip install Pillow
有效使用技巧
- 1. 保留原始文件:切勿覆盖源图像
- 使用合适的格式:照片用 JPEG,图形用 PNG
- 优化质量:平衡质量与文件大小
- 批量操作:处理多张图像时使用批量处理器
- 查阅参考文档:高级操作请参考文档
- 验证输入:处理前检查图像格式和大小
- 先测试:批量处理前先对单张图像进行测试
局限性
- - 仅限于 2D 图像处理(不支持视频)
- 某些 EXIF 数据可能无法在所有格式中保留
- 字体可用性可能因系统而异
- 超大图像可能需要大量内存
- 高级照片编辑(图层、蒙版)需要专业工具
故障排除
导入错误:确保已安装 Pillow(pip install Pillow)
字体未找到:水印脚本会回退到默认字体
内存错误:将大图像分批处理
格式错误:检查输入图像格式兼容性
RGBA 转 JPEG:脚本会自动处理 RGBA→RGB 转换
有关详细操作和故障排除,请始终参考