ImageMagick 使用大全:从入门到精通

ImageMagick 使用大全:从入门到精通

ImageMagick 是一款功能强大的开源图像处理软件套件,它支持超过 200 种图像格式,提供了丰富的命令行工具,它可以读取、转换和写入多种格式的图像,能够进行图像格式转换、编辑、合成等操作。无论是简单的格式转换还是复杂的图像处理,ImageMagick 都能胜任。ImageMagick 的主要特点是能够通过命令行进行批量处理,非常适合自动化图像处理任务。

安装

Windows

  1. 访问 ImageMagick 官网
  2. 下载 Windows 版本的安装包
  3. 运行安装程序并按照提示完成安装
  4. 确保将 ImageMagick 添加到系统 PATH

macOS

1
2
# 使用 Homebrew 安装
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 包含多个命令行工具,最常用的有:

  1. convert:图像格式转换和处理
  2. mogrify:批量处理图像(直接修改原文件)
  3. identify:显示图像信息
  4. composite:图像合成
  5. 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
# JPEG 转 PNG
convert input.jpg output.png

# PNG 转 WebP
convert input.png output.webp

# 批量转换当前目录所有 JPG 为 PNG
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
# 简单裁剪:从坐标 (10,10) 开始裁剪 200x200 区域
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
# 旋转 90 度
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
# 调整 JPEG 质量(1-100)
convert input.jpg -quality 85 output.jpg

# 压缩 PNG
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

# sepia 怀旧效果
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
# 从多个图像创建 GIF
convert -delay 20 -loop 0 *.jpg animation.gif

# 调整 GIF 大小
convert animation.gif -resize 400x300 resized_animation.gif

# 优化 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!如有任何问题或建议,欢迎留言讨论。