imagemagick-conversion
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseImageMagick 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 command to perform common image conversion and manipulation tasks.
magickKey Command Pattern:
bash
magick input-file [options] output-fileImageMagick 是一款功能强大的命令行图像处理工具,本技能提供使用 命令完成常见图片转换和处理任务的指导。
magick核心命令格式:
bash
magick input-file [options] output-fileCommon Use Cases
常见使用场景
Format Conversion
格式转换
Basic format conversion:
bash
magick image.jpg image.png
magick photo.png photo.webpBatch convert all JPEGs to PNG:
bash
magick mogrify -format png *.jpgConvert 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/ *.jpgResizing Images
调整图片尺寸
Resize by percentage:
bash
magick image.jpg -resize 50% output.jpgResize to specific width (maintain aspect ratio):
bash
magick image.jpg -resize 800x output.jpgResize to specific height (maintain aspect ratio):
bash
magick image.jpg -resize x600 output.jpgResize to fit within dimensions (maintain aspect ratio):
bash
magick image.jpg -resize 800x600 output.jpgResize to exact dimensions (ignore aspect ratio):
bash
magick image.jpg -resize 800x600! output.jpgResize only if larger:
bash
magick image.jpg -resize '800x600>' output.jpgResize 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.jpgQuality and Compression
质量与压缩设置
Set JPEG quality (1-100, default 92):
bash
magick image.jpg -quality 85 output.jpgOptimize PNG compression:
bash
magick image.png -quality 95 output.pngCreate 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.webpThumbnails
缩略图生成
Generate thumbnail (fast, lower quality):
bash
magick image.jpg -thumbnail 200x200 thumb.jpgGenerate 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.jpgBatch Operations
批量操作
Resize all images in directory:
bash
magick mogrify -resize 800x600 -path resized/ *.jpgConvert and resize in one operation:
bash
magick mogrify -resize 1200x -format webp -quality 85 -path output/ *.jpgProcess 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.jpgDetailed image information:
bash
magick identify -verbose image.jpg展示图片基础信息:
bash
magick identify image.jpg展示图片详细信息:
bash
magick identify -verbose image.jpgAdvanced Transformations
高级变换
Rotate image:
bash
magick image.jpg -rotate 90 rotated.jpgFlip horizontally:
bash
magick image.jpg -flop flipped.jpgFlip vertically:
bash
magick image.jpg -flip flipped.jpgCrop to specific region:
bash
magick image.jpg -crop 800x600+100+100 cropped.jpgAuto-orient based on EXIF:
bash
magick image.jpg -auto-orient output.jpgStrip 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.jpgImportant Notes
重要说明
mogrify vs convert
mogrify 与 convert 的区别
-
: Modifies files in-place or writes to specified path
magick mogrify- Use option to preserve originals
-path - Efficient for batch operations
- Use
-
(or just
magick convert): Creates new filesmagick- Always preserves original
- Better for single-file operations
-
:直接修改原文件或写入到指定路径
magick mogrify- 使用 参数可以保留原文件
-path - 适合批量操作,效率更高
- 使用
-
(或直接使用
magick convert):生成新文件magick- 始终保留原文件
- 更适合单文件操作
Performance Tips
性能优化提示
- Use for thumbnails: Faster than
-thumbnailfor small previews-resize - Use to remove metadata: Reduces file size significantly
-strip - Batch operations: Process multiple files in one command
mogrify - Quality settings: 85-90 is usually optimal for JPEG (balances size/quality)
- 生成缩略图使用 :比
-thumbnail生成小预览图的速度更快-resize - 使用 移除元数据:可以大幅减小文件体积
-strip - 优先批量操作:在一个 命令中处理多个文件
mogrify - 质量设置建议: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
undefinedCreate 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` 参数保留原文件:**
```bashThis preserves originals in current directory
该命令会保留当前目录下的原文件
magick mogrify -resize 800x -path resized/ *.jpg
**Quote wildcards in shell:**
```bashmagick mogrify -resize 800x -path resized/ *.jpg
**Shell 中通配符加引号:**
```bashPrevents premature shell expansion
避免 Shell 提前展开通配符
magick mogrify -resize '800x600>' -path output/ '*.jpg'
undefinedmagick mogrify -resize '800x600>' -path output/ '*.jpg'
undefinedCommon Patterns
常用处理模式
Web Optimization Workflow
Web 优化工作流
bash
undefinedbash
undefinedCreate 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
undefinedmagick mogrify -strip web-optimized/*.webp
undefinedThumbnail Generation
缩略图生成
bash
undefinedbash
undefinedCreate 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
undefinedfor img in *.jpg; do
magick "$img" -thumbnail 300x300 -background white -gravity center -extent 300x300 "thumbnails/${img%.jpg}_thumb.jpg"
done
undefinedMulti-Format Export
多格式导出
bash
undefinedbash
undefinedExport 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
undefinedmkdir -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
undefinedTroubleshooting
故障排查
Check ImageMagick version:
bash
magick -versionVerify supported formats:
bash
magick identify -list formatTest command on single file first:
bash
undefined检查 ImageMagick 版本:
bash
magick -version查看支持的格式列表:
bash
magick identify -list format先在单个文件上测试命令:
bash
undefinedAlways test before batch operations
批量操作前务必先测试
magick test-image.jpg -resize 50% test-output.jpg
undefinedmagick test-image.jpg -resize 50% test-output.jpg
undefinedWhen 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 command is typically available via Homebrew () or system package managers.
magickbrew install imagemagick本技能可以和其他开发工作流互补:
- Web 开发:优化部署用的图片资源
- 文档编写:生成截图和示意图
- CI/CD:在流水线中自动化处理图片
- 内容创作:为不同平台准备适配的图片资源
magickbrew install imagemagick