ImageMagick 使用大全:从入门到精通
ImageMagick 是一款功能强大的开源图像处理软件套件,它支持超过 200 种图像格式,提供了丰富的命令行工具,它可以读取、转换和写入多种格式的图像,能够进行图像格式转换、编辑、合成等操作。无论是简单的格式转换还是复杂的图像处理,ImageMagick 都能胜任。ImageMagick 的主要特点是能够通过命令行进行批量处理,非常适合自动化图像处理任务。
安装
Windows
- 访问 ImageMagick 官网
- 下载 Windows 版本的安装包
- 运行安装程序并按照提示完成安装
- 确保将 ImageMagick 添加到系统 PATH
macOS
1 2
|
brew install imagemagick
|
Linux (Ubuntu/Debian)
1 2
|
sudo apt-get update sudo apt-get install imagemagick
|
Linux (CentOS/RHEL)
1 2 3
|
sudo yum install ImageMagick
sudo dnf install ImageMagick
|
工具介绍
ImageMagick 包含多个命令行工具,最常用的有:
- convert:图像格式转换和处理
- mogrify:批量处理图像(直接修改原文件)
- identify:显示图像信息
- composite:图像合成
- montage:创建图像网格
基本语法
ImageMagick 命令的基本格式:
1
|
command [options] input_file output_file
|
例如,将 JPEG 转换为 PNG:
1
|
convert image.jpg image.png
|
常见用例
1. 格式转换
1 2 3 4 5 6 7 8
|
convert input.jpg output.png
convert input.png output.webp
mogrify -format png *.jpg
|
2. 调整大小
1 2 3 4 5 6 7 8 9 10 11
|
convert input.jpg -resize 800 output.jpg
convert input.jpg -resize 800x600 output.jpg
convert input.jpg -resize 800x600! output.jpg
convert input.jpg -resize 100x100^ -gravity center -extent 100x100 thumbnail.jpg
|
3. 裁剪图像
1 2 3 4 5 6 7 8
|
convert input.jpg -crop 200x200+10+10 output.jpg
convert input.jpg -trim output.jpg
convert input.jpg -crop 500x300+100+50 -resize 200x200 output.jpg
|
4. 旋转和翻转
1 2 3 4 5 6 7 8
|
convert input.jpg -rotate 90 output.jpg
convert input.jpg -flip output.jpg
convert input.jpg -flop output.jpg
|
5. 添加水印
1 2 3 4 5
|
convert input.jpg -pointsize 36 -fill white -gravity southeast -annotate +10+10 "Copyright" output.jpg
composite -gravity southeast watermark.png input.jpg output.jpg
|
6. 图像优化
1 2 3 4 5 6 7 8
|
convert input.jpg -quality 85 output.jpg
convert input.png -strip -quality 85 output.png
convert input.png -colors 64 output.png
|
7. 添加效果和滤镜
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
convert input.jpg -blur 0x8 output.jpg
convert input.jpg -sharpen 0x5 output.jpg
convert input.jpg -monochrome output.jpg
convert input.jpg -sepia-tone 80% output.jpg
convert input.jpg -bordercolor black -border 10x10 output.jpg
|
8. 批量处理
1 2 3 4 5 6 7 8
|
mogrify -resize 800x600 *.jpg
for file in *.jpg; do composite -gravity southeast watermark.png "$file" "watermarked_$file"; done
mogrify -format png -resize 50% *.jpg
|
9. 创建 GIF 动画
1 2 3 4 5 6 7 8
|
convert -delay 20 -loop 0 *.jpg animation.gif
convert animation.gif -resize 400x300 resized_animation.gif
convert animation.gif -layers optimize optimized_animation.gif
|
10. 图像信息
1 2 3 4 5 6 7 8
|
identify image.jpg
identify -format "%f: %m %wx%h %b\n" *.jpg
identify -regard-warnings image.jpg
|
命令速查表
| 功能 |
命令 |
| 格式转换 |
convert input.jpg output.png |
| 调整大小 |
convert input.jpg -resize 800x600 output.jpg |
| 裁剪 |
convert input.jpg -crop WxH+X+Y output.jpg |
| 旋转 |
convert input.jpg -rotate 90 output.jpg |
| 添加文字 |
convert input.jpg -pointsize 36 -annotate +X+Y "Text" output.jpg |
| 模糊 |
convert input.jpg -blur 0x8 output.jpg |
| 锐化 |
convert input.jpg -sharpen 0x5 output.jpg |
| 批量处理 |
mogrify -option *.jpg |
| 图像信息 |
identify image.jpg |
总结
ImageMagick 是一个功能强大且灵活的图像处理工具,特别适合批量处理和自动化任务。通过掌握这些基本命令和技巧,你可以高效地完成各种图像处理工作。随着使用的深入,你会发现 ImageMagick 还有更多高级功能等待探索。
更多资源
希望这份指南能帮助你更好地使用 ImageMagick!如有任何问题或建议,欢迎留言讨论。