imagemagick-conversion

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ImageMagick Image Conversion

ImageMagick 图片转换

Project: Project-independent Gitignored: Yes
项目: 无项目依赖 Git 忽略:

Trigger

触发条件

Use this skill when users request image manipulation tasks including:
  • Converting between image formats (PNG, JPEG, WebP, GIF, TIFF, etc.)
  • Resizing images (dimensions, percentages, aspect ratios)
  • Batch processing multiple images
  • Adjusting image quality and compression
  • Creating thumbnails
  • Basic image transformations (rotate, flip, crop)
当用户提出以下图片处理需求时使用本技能:
  • 不同图片格式(PNG、JPEG、WebP、GIF、TIFF 等)之间的转换
  • 调整图片大小(指定尺寸、百分比、保持宽高比)
  • 批量处理多张图片
  • 调整图片质量和压缩参数
  • 生成缩略图
  • 基础图像变换(旋转、翻转、裁剪)

Overview

概述

ImageMagick is a powerful command-line tool for image processing. This skill provides guidance for using the
magick
command to perform common image conversion and manipulation tasks.
Key Command Pattern:
bash
magick input-file [options] output-file
ImageMagick 是一款功能强大的命令行图像处理工具,本技能提供使用
magick
命令完成常见图片转换和处理任务的指导。
核心命令格式:
bash
magick input-file [options] output-file

Common Use Cases

常见使用场景

Format Conversion

格式转换

Basic format conversion:
bash
magick image.jpg image.png
magick photo.png photo.webp
Batch convert all JPEGs to PNG:
bash
magick mogrify -format png *.jpg
Convert with specific output directory:
bash
mkdir -p output
magick mogrify -format webp -path output/ *.jpg
基础格式转换:
bash
magick image.jpg image.png
magick photo.png photo.webp
批量将所有 JPEG 转换为 PNG:
bash
magick mogrify -format png *.jpg
转换并输出到指定目录:
bash
mkdir -p output
magick mogrify -format webp -path output/ *.jpg

Resizing Images

调整图片尺寸

Resize by percentage:
bash
magick image.jpg -resize 50% output.jpg
Resize to specific width (maintain aspect ratio):
bash
magick image.jpg -resize 800x output.jpg
Resize to specific height (maintain aspect ratio):
bash
magick image.jpg -resize x600 output.jpg
Resize to fit within dimensions (maintain aspect ratio):
bash
magick image.jpg -resize 800x600 output.jpg
Resize to exact dimensions (ignore aspect ratio):
bash
magick image.jpg -resize 800x600! output.jpg
Resize only if larger:
bash
magick image.jpg -resize '800x600>' output.jpg
Resize only if smaller:
bash
magick image.jpg -resize '800x600<' output.jpg
按百分比缩放:
bash
magick image.jpg -resize 50% output.jpg
缩放到指定宽度(保持宽高比):
bash
magick image.jpg -resize 800x output.jpg
缩放到指定高度(保持宽高比):
bash
magick image.jpg -resize x600 output.jpg
缩放到适配指定尺寸范围内(保持宽高比):
bash
magick image.jpg -resize 800x600 output.jpg
缩放到 exact 尺寸(忽略宽高比):
bash
magick image.jpg -resize 800x600! output.jpg
仅当图片更大时才缩放:
bash
magick image.jpg -resize '800x600>' output.jpg
仅当图片更小时才缩放:
bash
magick image.jpg -resize '800x600<' output.jpg

Quality and Compression

质量与压缩设置

Set JPEG quality (1-100, default 92):
bash
magick image.jpg -quality 85 output.jpg
Optimize PNG compression:
bash
magick image.png -quality 95 output.png
Create high-quality WebP:
bash
magick image.jpg -quality 90 output.webp
设置 JPEG 质量(1-100,默认92):
bash
magick image.jpg -quality 85 output.jpg
优化 PNG 压缩:
bash
magick image.png -quality 95 output.png
生成高质量 WebP:
bash
magick image.jpg -quality 90 output.webp

Thumbnails

缩略图生成

Generate thumbnail (fast, lower quality):
bash
magick image.jpg -thumbnail 200x200 thumb.jpg
Generate thumbnail with padding:
bash
magick image.jpg -thumbnail 200x200 -background white -gravity center -extent 200x200 thumb.jpg
生成缩略图(速度快、质量较低):
bash
magick image.jpg -thumbnail 200x200 thumb.jpg
生成带内边距的缩略图:
bash
magick image.jpg -thumbnail 200x200 -background white -gravity center -extent 200x200 thumb.jpg

Batch Operations

批量操作

Resize all images in directory:
bash
magick mogrify -resize 800x600 -path resized/ *.jpg
Convert and resize in one operation:
bash
magick mogrify -resize 1200x -format webp -quality 85 -path output/ *.jpg
Process specific file types:
bash
magick mogrify -resize 50% -path smaller/ *.{jpg,png,gif}
调整目录下所有图片的尺寸:
bash
magick mogrify -resize 800x600 -path resized/ *.jpg
单次操作完成转换和尺寸调整:
bash
magick mogrify -resize 1200x -format webp -quality 85 -path output/ *.jpg
处理指定类型的文件:
bash
magick mogrify -resize 50% -path smaller/ *.{jpg,png,gif}

Image Information

图片信息查询

Display image information:
bash
magick identify image.jpg
Detailed image information:
bash
magick identify -verbose image.jpg
展示图片基础信息:
bash
magick identify image.jpg
展示图片详细信息:
bash
magick identify -verbose image.jpg

Advanced Transformations

高级变换

Rotate image:
bash
magick image.jpg -rotate 90 rotated.jpg
Flip horizontally:
bash
magick image.jpg -flop flipped.jpg
Flip vertically:
bash
magick image.jpg -flip flipped.jpg
Crop to specific region:
bash
magick image.jpg -crop 800x600+100+100 cropped.jpg
Auto-orient based on EXIF:
bash
magick image.jpg -auto-orient output.jpg
Strip metadata (reduce file size):
bash
magick image.jpg -strip output.jpg
旋转图片:
bash
magick image.jpg -rotate 90 rotated.jpg
水平翻转:
bash
magick image.jpg -flop flipped.jpg
垂直翻转:
bash
magick image.jpg -flip flipped.jpg
裁剪到指定区域:
bash
magick image.jpg -crop 800x600+100+100 cropped.jpg
根据 EXIF 信息自动调整方向:
bash
magick image.jpg -auto-orient output.jpg
移除元数据(减小文件体积):
bash
magick image.jpg -strip output.jpg

Important Notes

重要说明

mogrify vs convert

mogrify 与 convert 的区别

  • magick mogrify
    : Modifies files in-place or writes to specified path
    • Use
      -path
      option to preserve originals
    • Efficient for batch operations
  • magick convert
    (or just
    magick
    ): Creates new files
    • Always preserves original
    • Better for single-file operations
  • magick mogrify
    :直接修改原文件或写入到指定路径
    • 使用
      -path
      参数可以保留原文件
    • 适合批量操作,效率更高
  • magick convert
    (或直接使用
    magick
    ):生成新文件
    • 始终保留原文件
    • 更适合单文件操作

Performance Tips

性能优化提示

  1. Use
    -thumbnail
    for thumbnails
    : Faster than
    -resize
    for small previews
  2. Use
    -strip
    to remove metadata
    : Reduces file size significantly
  3. Batch operations: Process multiple files in one
    mogrify
    command
  4. Quality settings: 85-90 is usually optimal for JPEG (balances size/quality)
  1. 生成缩略图使用
    -thumbnail
    :比
    -resize
    生成小预览图的速度更快
  2. 使用
    -strip
    移除元数据
    :可以大幅减小文件体积
  3. 优先批量操作:在一个
    mogrify
    命令中处理多个文件
  4. 质量设置建议:JPEG 质量设置为85-90通常可以兼顾体积和质量

Format Recommendations

格式选择建议

  • JPEG: Photos, complex images with gradients (lossy)
  • PNG: Screenshots, graphics with transparency (lossless)
  • WebP: Modern format, excellent compression (lossy or lossless)
  • GIF: Simple animations, limited colors
  • TIFF: Archival, high-quality storage
  • JPEG:照片、带渐变的复杂图像(有损压缩)
  • PNG:截图、带透明度的图形(无损压缩)
  • WebP:现代格式,压缩效率极高(支持有损/无损压缩)
  • GIF:简单动画、色彩有限的图像
  • TIFF:归档存储、高质量文件存储

Safety Considerations

安全注意事项

Always test commands on copies first:
bash
undefined
务必先在副本上测试命令:
bash
undefined

Create test directory

创建测试目录

mkdir -p test-output
mkdir -p test-output

Test on single file

先在单个文件上测试

magick original.jpg -resize 50% test-output/test.jpg
magick original.jpg -resize 50% test-output/test.jpg

Verify result before batch processing

批量处理前先验证结果


**Use `-path` with mogrify to preserve originals:**
```bash

**使用 mogrify 时添加 `-path` 参数保留原文件:**
```bash

This preserves originals in current directory

该命令会保留当前目录下的原文件

magick mogrify -resize 800x -path resized/ *.jpg

**Quote wildcards in shell:**
```bash
magick mogrify -resize 800x -path resized/ *.jpg

**Shell 中通配符加引号:**
```bash

Prevents premature shell expansion

避免 Shell 提前展开通配符

magick mogrify -resize '800x600>' -path output/ '*.jpg'
undefined
magick mogrify -resize '800x600>' -path output/ '*.jpg'
undefined

Common Patterns

常用处理模式

Web Optimization Workflow

Web 优化工作流

bash
undefined
bash
undefined

Create optimized versions for web

生成适合 Web 使用的优化版本

mkdir -p web-optimized
mkdir -p web-optimized

Convert to WebP with quality 85, resize to max 1920px width

转换为 WebP 格式,质量85,最大宽度限制为1920px

magick mogrify -resize 1920x -quality 85 -format webp -path web-optimized/ *.jpg
magick mogrify -resize 1920x -quality 85 -format webp -path web-optimized/ *.jpg

Strip metadata to reduce size

移除元数据减小体积

magick mogrify -strip web-optimized/*.webp
undefined
magick mogrify -strip web-optimized/*.webp
undefined

Thumbnail Generation

缩略图生成

bash
undefined
bash
undefined

Create thumbnail directory

创建缩略图目录

mkdir -p thumbnails
mkdir -p thumbnails

Generate 300x300 thumbnails with white padding

生成300x300、带白色内边距的缩略图

for img in *.jpg; do magick "$img" -thumbnail 300x300 -background white -gravity center -extent 300x300 "thumbnails/${img%.jpg}_thumb.jpg" done
undefined
for img in *.jpg; do magick "$img" -thumbnail 300x300 -background white -gravity center -extent 300x300 "thumbnails/${img%.jpg}_thumb.jpg" done
undefined

Multi-Format Export

多格式导出

bash
undefined
bash
undefined

Export to multiple formats for compatibility

导出为多种格式用于兼容不同场景

mkdir -p exports/{png,webp,jpg}
for img in source/*.png; do name=$(basename "$img" .png) magick "$img" -quality 90 "exports/png/$name.png" magick "$img" -quality 85 "exports/webp/$name.webp" magick "$img" -quality 85 "exports/jpg/$name.jpg" done
undefined
mkdir -p exports/{png,webp,jpg}
for img in source/*.png; do name=$(basename "$img" .png) magick "$img" -quality 90 "exports/png/$name.png" magick "$img" -quality 85 "exports/webp/$name.webp" magick "$img" -quality 85 "exports/jpg/$name.jpg" done
undefined

Troubleshooting

故障排查

Check ImageMagick version:
bash
magick -version
Verify supported formats:
bash
magick identify -list format
Test command on single file first:
bash
undefined
检查 ImageMagick 版本:
bash
magick -version
查看支持的格式列表:
bash
magick identify -list format
先在单个文件上测试命令:
bash
undefined

Always test before batch operations

批量操作前务必先测试

magick test-image.jpg -resize 50% test-output.jpg
undefined
magick test-image.jpg -resize 50% test-output.jpg
undefined

When to Use This Skill

适用场景

✓ Use this skill for:
  • Format conversions between standard image types
  • Resizing operations (dimensions, percentages)
  • Quality adjustments and compression
  • Batch processing workflows
  • Generating thumbnails or previews
  • Basic transformations (rotate, crop, flip)
✗ Don't use this skill for:
  • Advanced photo editing (use GIMP, Photoshop)
  • Complex filters or effects (consider dedicated tools)
  • Video processing (use FFmpeg)
  • Vector graphics (use Inkscape, Illustrator)
✓ 适合使用本技能的场景:
  • 标准图片类型之间的格式转换
  • 尺寸调整操作(指定尺寸、百分比缩放)
  • 质量调整和压缩设置
  • 批量处理工作流
  • 生成缩略图或预览图
  • 基础变换操作(旋转、裁剪、翻转)
✗ 不适合使用本技能的场景:
  • 高级修图需求(使用 GIMP、Photoshop)
  • 复杂滤镜或特效处理(使用专业工具)
  • 视频处理需求(使用 FFmpeg)
  • 矢量图形处理(使用 Inkscape、Illustrator)

Integration with Workflows

工作流集成

This skill complements other development workflows:
  • Web development: Optimize images for deployment
  • Documentation: Generate screenshots and diagrams
  • CI/CD: Automate image processing in pipelines
  • Content creation: Prepare images for various platforms
The
magick
command is typically available via Homebrew (
brew install imagemagick
) or system package managers.
本技能可以和其他开发工作流互补:
  • Web 开发:优化部署用的图片资源
  • 文档编写:生成截图和示意图
  • CI/CD:在流水线中自动化处理图片
  • 内容创作:为不同平台准备适配的图片资源
magick
命令通常可以通过 Homebrew(
brew install imagemagick
)或系统包管理器安装。